PDFLib: Inserting a Google Map into a PDF with Geolocation

January 14, 2010

I thought this was an interesting enough to share with everyone. I’m working on a project that involves using PDFLIb to create PDF documents. And one of the requirements was to insert a Google Map image with a marker where a property is located. Here’s how we do that.

PDFLib is a very powerful program for creating PDF documents. I’ve used it before in other projects and when the need arose to make more PDF documents on a massive scale I opted to use PDFLib again.

This time I needed to dynamically insert a Google Map of a property into a PDF. However with a little PHP & PDFLib this task isn’t so daunting.  If anyone is interested in me going more into detail on how the code works please leave some comments.

<?php

// get the map image from google
$addressfull = “address goes here”; // use this format for your address 100+ne+5th+st,city,state+zip
$map_zoom = “14″;
$map_size = “600×600″;
$map_key = “generate a google api key from the google website and insert here”;
$map_type = “roadmap”;
$map_markers = “color:red|color:red|$addressfull”;
$google_link = “http://maps.google.com/maps/api/staticmap?center=$addressfull&zoom=$map_zoom&”;
$google_link .=  ”size=$map_size&maptype=$map_type&markers=$map_markers&sensor=false&key=$map_key”;
$map_link = str_replace(” “,”+”,$google_link); 
$map = file_get_contents($map_link);

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// begin pdf
try {
    $p = new PDFlib();
  
    # This means we must check return values of load_font() etc.
    $p->set_parameter(“errorpolicy”, “return”);

    /* This line is required to avoid problems on Japanese systems */
    $p->set_parameter(“hypertextencoding”, “winansi”);

    /*  open new PDF file; insert a file name to create the PDF on disk */
    $outfile = “/var/www/pip/temp/”. $folder .”.pdf”;
    if ($p->begin_document(“”, “linearize=true inmemory=true optimize=true”) == 0) { // $outfile
  die(“Error: ” . $p->get_errmsg());
    }
      
    $p->set_info(“Creator”, “Map Maker”);
    $p->set_info(“Author”, “ME”);
    $p->set_info(“Title”, “My Title”);
    $p->set_info(“Subject”, “Subject”);
    $p->set_info(“Keywords”, “keywords”);

 $p->begin_page_ext(612, 828, “”);
  
 // create a pdf virtual file of the map for display
 $mappvf = “/pvf/map.png”;
 $p->create_pvf($mappvf,$map,”");
    
 // load image
 $image = $p->load_image(“auto”, $mappvf, “”);
 if (!$image) {
  die(“Error Map: ” . $p->get_errmsg());
 }
 $p->fit_image($image, 56, 250, “boxsize={500 400} fitmethod={meet} position={center}”);
 $p->close_image($image);
 $p->delete_pvf($mappvf);
         
 $p->end_page_ext(“”);

 // end document
 $p->end_document(“”);
  
 $buf = $p->get_buffer();
 $len = strlen($buf);
 
 header(“Content-type: application/pdf”);
 header(“Content-Length: $len”);
 header(“Content-Disposition: inline; filename=”. $assetnum_address .”.pdf”);
 print $buf;
  
 }
 catch (PDFlibException $e) {
     die(“PDFlib exception occurred in hello sample:\n” .
  ”[" . $e->get_errnum() . "] ” . $e->get_apiname() . “: ” .
  $e->get_errmsg() . “\n”);
 }
 catch (Exception $e) {
     die($e);
 }
 
 $p = 0;
 
 flush();

?>

Please comment if you have questions.

One Response to “PDFLib: Inserting a Google Map into a PDF with Geolocation”

  1. PDFLib: Inserting a Google Map into a PDF with Geolocation : Shut … | Drakz Free Online Service on January 14th, 2010 9:39 pm

    [...] here: PDFLib: Inserting a Google Map into a PDF with Geolocation : Shut … Share and [...]

Got something to say?

You must be logged in to post a comment.