Part of the SiteQuarters Network  
 
 

Online PDF Printer

Generate Dynamic PDFs From Any Web-Enabled Application

Sign up for FREE and start printing PDFs right now!

Show me some examples

Online PDF Printer doesn't require that you use any specific scripting or programming language; as long as you can POST an array of data to a URL, you're good to go. The following examples are in PHP, but .NET, Java, Perl, Ruby, et al would all work just as well.

We currently only support Word documents (.doc and .docx) as document templates. In the future, we also plan on supporting OpenOffice formats.

The following examples will make use of some basic functions for preparing and sending data:

$url = "http://print.onlinepdfprinter.com";
function do_post_request($url, $data) {

  $data     = array("options" => base64_encode(json_encode($data)));

  $ch       = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  $response = curl_exec($ch);

  return $response;

} // end do_post_request()
function force_pdf_download($stream, $filename_to_save_as = "output.pdf") {

  if ($stream && (substr($stream, 0, 5) == "%PDF-")) {

    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Length: ".strlen($stream));
    header("Content-Type: application/pdf");
    header("Content-Transfer-Encoding: binary");
    header("Content-Disposition: attachment; filename=\"${filename_to_save_as}\"");

    echo $stream;

  } // end if

} // end force_pdf_download()

Example 1: A single-page document with static data

Online PDF Printer works with both static and dynamic data:

  • Static - a single array (row) of data would be used for document elements like a date in a header or footer.
     
  • Dynamic - an array of rows (associative arrays) that mimic a database row (ex. "field_name" => $value); in actual practice, this could be used to populate a table of data in a single document template, or even a single-row-per-page template for something like batch-printing accreditation certificates (where only a name changes on each page)

Variable replacements are set up in Word templates, capitalized and enclosed by curly braces; for instance, if you have a static variable called name, your template would contain {NAME}, date would be {DATE} and so on.

The following data will be passed into the template to be populated:

$static_data = array(

  "date"           => strftime("%Y-%m-%d", time()),
  "contact_id"     => "My Company",
  "address"        => "123 Fake St",
  "city"           => "Columbus",
  "state"          => "OH",
  "zip"            => "43215",
  "project_number" => "#643",
  "project_name"   => "Concrete Delivery",
  "description"    => "Lorem ipsum dolor sit amet, consectetur 

                       adipiscing elit. Nunc venenatis lacinia diam. 

                       Curabitur tempus. Maecenas imperdiet imperdiet ante.",

);

Code from your application:

$options = array( // template points to Word template
                 "template"      => "example_01.doc",
                 "api_key"       => "YOUR API KEY",
                 "static_data"   => $static_data,
                 "rows_per_page" => 1,
           );

$pdf_data = do_post_request($url, $options);

$filename = "work_order_authorization." . strftime("%Y-%m-%d", time()) . ".pdf";

force_pdf_download($pdf_data, $filename);
Download Word Template
Download Generated PDF

Example 2: An N-page document with static and dynamic data

This example contains both static and dynamic data. Dynamic data comes in the form of an array of rows (associative arrays) that mimic a database row (ex. "field_name" => $value).

Dynamic data templates can be slightly tricky; part of the reason for using Online PDF Printer is to easily support print-specific requirements for generated PDFs. In other words, if your boss wants a dynamic report with a cover page, or a constant header on every page above your table, most non-word processing systems (HTML, etc) don't allow you to insert page breaks or manipulate print margins.

Online PDF Printer does, by nature of working directly off of documents formatted by a word processor.

Because of this, you have to construct your templates with available space in mind. In this example, you could be pushing 5 rows or 500 rows to the printable template. The document becomes much more professional and official if we can put a consistent header, page numbers, etc into the template.

It's designed to allow 20 rows per page; as long as you specify how many rows are available on a given page of a document template, Online PDF Printer handles the rest automatically.

Dynamic variable replacements are specified by their field name and by the available space. We've allowed for 20 rows of data to be on any page at any time. If you send 35 rows to be printed, you will get a 2 page PDF back as your data stream.

Variable replacements are specified in the format {FIELDNAME_ROWNUMBER}; an example would be {NAME_14}. If $dynamic_rows contains 14 rows and the 14th row has the array key name, the value of $dynamic_rows[13]["name"] is what {NAME_14} gets replaced with.

Note that dynamic template replacements start numbering at 1 and not 0 like an array does. We do this because the person creating the template isn't always the developer, and it takes less time to explain what an array is and why it needs to index than to just say "start numbering multiple rows at 1".

The following data will be passed into the template to be populated:

$static_data = array(

  "company_name" => "My Company's",
  "date_printed" => strftime("%b %d, %Y", time()),

);

$dynamic_rows = array(
  array("name"    => "John Peterman",
        "company" => "123 Store",
        "address" => "987 Fake St, Columbus, Ohio 43201",
        "home"    => "(833) 752-3628",
        "mobile"  => "(822) 633-8990",
        "fax"     => "",
        "email"   => "john@peterman.com",
  ),
  array("name"    => "Bob Jefferson",
        "company" => "",
        "address" => "N/A",
        "home"    => "",
        "mobile"  => "",
        "fax"     => "",
        "email"   => "bjeff@msn.com",
  ),
  array("name"    => "Lonnie Andrews",
        "company" => "LonLand",
        "address" => "",
        "home"    => "(822) 489-0703",
        "mobile"  => "",
        "fax"     => "(844) 224-0282",
        "email"   => "Lonnie@lonland.ru",
  ),
  array("name"    => "Farquar Monroe",
        "company" => "",
        "address" => "1274 State Ave, Detroit, Michigan 12345",
        "home"    => "",
        "mobile"  => "(855) 576-7263",
        "fax"     => "",
        "email"   => "fqm@fqm.biz",
  ),
  array("name"    => "Bill Trunk",
        "company" => "T Malletier",
        "address" => "75008 Champs-Elysées, Paris, France ",
        "home"    => "(811) 686-8599",
        "mobile"  => "(844) 073-6623",
        "fax"     => "",
        "email"   => "btrunk@tmalletier.fr",
  ),
);

Code from your application:

$options = array("template"      => "example_02.doc", // points to Word template
                 "api_key"       => "YOUR API KEY",
                 "static_data"   => $static_data,
                 "dynamic_rows"  => $dynamic_rows,
                 "rows_per_page" => 20,
           );

$pdf_data = do_post_request($url, $options);

$filename = "contact_list." . strftime("%Y-%m-%d", time()) . ".pdf";

force_pdf_download($pdf_data, $filename);
Download Word Template
Download Generated PDF

Example 3: A multi-template combination document

This example shows you how you can use Online PDF Printer to populate multiple document templates and combine them into a single PDF document.

It's very easy to break a document up into sections that are then combined with each other in the final PDF stream. For instance, you might have static, 1-page cover and footer pages, with two separate N-page dynamic sections in the middle of the document; by preparing each part of the document as a separate template and POSTing static and dynamic data, plus rows per page, for each specific template, Online PDF Printer fills everything out and returns a single PDF stream ready for download or printing.

In this example, we have a static cover page and three N-page documents. This method can also be used to print a date range of data for a single document template, combined into a single PDF download for the end user.

Documents are combined in the order that they are specified in $options["template"].

The following data will be passed into the template to be populated:

$options = array (
  "static_data" => 
  array (
    "example_03.page_01.doc" => 
    array (
      "date"               => "October 8, 2007",
      "superintendant"     => "John Doe",
      "initialed_by"       => "test",
      "project_name"       => "Sample Job",
      "project_number"     => "#00000",
      "weather_temp_am"    => "55 F",
      "weather_temp_pm"    => "65 f",
      "weather_conditions" => "Cold/Light snow last night",
      "inspections"        => "N/A",
      "problems"           => "N/A",
      "general_comments"   => "Flooring, Exterior trim, Electrical",
    ),
    "example_03.page_02.doc" => 
    array (
      "date"               => "October 8, 2007",
      "superintendant"     => "John Doe",
      "initialed_by"       => "test",
      "project_name"       => "Sample Job",
      "project_number"     => "#00000",
      "weather_temp_am"    => "55 F",
      "weather_temp_pm"    => "65 f",
      "weather_conditions" => "Cold/Light snow last night",
      "inspections"        => "N/A",
      "problems"           => "N/A",
      "general_comments"   => "Flooring, Exterior trim, Electrical",
    ),
    "example_03.page_03.doc" => 
    array (
      "date"               => "October 8, 2007",
      "superintendant"     => "John Doe",
      "initialed_by"       => "test",
      "project_name"       => "Sample Job",
      "project_number"     => "#00000",
      "weather_temp_am"    => "55 F",
      "weather_temp_pm"    => "65 f",
      "weather_conditions" => "Cold/Light snow last night",
      "inspections"        => "N/A",
      "problems"           => "N/A",
      "general_comments"   => "Flooring, Exterior trim, Electrical",
    ),
    "example_03.page_04.doc" => 
    array (
      "date"               => "October 8, 2007",
      "superintendant"     => "John Doe",
      "initialed_by"       => "test",
      "project_name"       => "Sample Job",
      "project_number"     => "#00000",
      "weather_temp_am"    => "55 F",
      "weather_temp_pm"    => "65 f",
      "weather_conditions" => "Cold/Light snow last night",
      "inspections"        => "N/A",
      "problems"           => "N/A",
      "general_comments"   => "Flooring, Exterior trim, Electrical",
    ),
  ),
  "dynamic_rows" => 
  array (
    "example_03.page_01.doc" => 
    array (
    ),
    "example_03.page_02.doc" => 
    array (
      1 => 
      array (
        "subcontractor" => "My Company, Inc",
        "activity_1"    => "01000 General Conditions",
        "num_men"       => "1",
        "num_hours"     => "2",
        "description_1" => "one",
      ),
      2 => 
      array (
        "subcontractor" => "The Door People",
        "activity_1"    => "02050 Soils Testing",
        "num_men"       => "3",
        "num_hours"     => "4",
        "description_1" => "two",
      ),
      3 => 
      array (
        "subcontractor" => "Rick Johnson",
        "activity_1"    => "03670 Saw Cutting",
        "num_men"       => "5",
        "num_hours"     => "6",
        "description_1" => "three",
      ),
      4 => 
      array (
        "subcontractor" => "Steve",
        "activity_1"    => "06400 Architectural Woodwork",
        "num_men"       => "7",
        "num_hours"     => "8",
        "description_1" => "four",
      ),
      5 => 
      array (
        "subcontractor" => "Steve Stevenson",
        "activity_1"    => "08430 Entrance Doors",
        "num_men"       => "9",
        "num_hours"     => "0",
        "description_1" => "five",
      ),
    ),
    "example_03.page_03.doc" => 
    array (
      1 => 
      array (
        "employee"      => "The Glass Company, Inc. ",
        "activity_2"    => "00100 Architect",
        "total_hours"   => "1",
        "description_2" => "two",
      ),
      2 => 
      array (
        "employee"      => "Roofy's Roofing, LLC",
        "activity_2"    => "06408 Ornamental Columns",
        "total_hours"   => "2",
        "description_2" => "four",
      ),
      3 => 
      array (
        "employee"      => "Bill Billmanship",
        "activity_2"    => "16100 Electrical Package",
        "total_hours"   => "3",
        "description_2" => "six",
      ),
      4 => 
      array (
        "employee"      => "",
        "activity_2"    => "",
        "total_hours"   => "",
        "description_2" => "",
      ),
      5 => 
      array (
        "employee"      => "",
        "activity_2"    => "",
        "total_hours"   => "",
        "description_2" => "",
      ),
    ),
    "example_03.page_04.doc" => 
    array (
      1 => 
      array (
        "company" => "Steve Stevenson",
        "contact" => "Steve",
        "phone"   => "1-467-228-5300",
        "time"    => "04:00pm",
        "notes"   => "We talked about things.",
      ),
      2 => 
      array (
        "company" => "Steve",
        "contact" => "Steve #2",
        "phone"   => "1-783-394-9977",
        "time"    => "05:30pm",
        "notes"   => "A discussion was had.",
      ),
      3 => 
      array (
        "company" => "",
        "contact" => "",
        "phone"   => "",
        "time"    => "12:00am",
        "notes"   => "",
      ),
      4 => 
      array (
        "company" => "",
        "contact" => "",
        "phone"   => "",
        "time"    => "12:00am",
        "notes"   => "",
      ),
    ),
  ),
  "rows_per_page" => 
  array (
    "example_03.page_01.doc" => 20,
    "example_03.page_02.doc" => 20,
    "example_03.page_03.doc" => 20,
    "example_03.page_04.doc" => 10,
  ),
);

Code from your application:

// $template points to Word templates, COMBINED IN THE ORDER SPECIFIED

$template = array("example_03.page_01.doc",
                  "example_03.page_02.doc",
                  "example_03.page_03.doc",
                  "example_03.page_04.doc",
                 );

$options  = array_merge($options,
                        array("use_multiple_templates" => true,
                              "break_on_new_page"      => false,
                              "template"               => $template,
                              "api_key"                => "YOUR API KEY",
                             )
            );

$pdf_data = do_post_request($url, $options);

$filename = "daily_log." . strftime("%Y-%m-%d", time()) . ".pdf";

force_pdf_download($pdf_data, $filename);
Download Page 1 Template
Download Page 2 Template
Download Page 3 Template
Download Page 4 Template
Download Generated PDF