| |
Online PDF Printer
Generate Dynamic PDFs From Any Web-Enabled Application
Online PDF Printer Overview
Welcome to the developer documentation for Online PDF Printer! We
provide a gateway that is used to generate dynamic PDFs on-demand and serve them
back to your web-enabled application.
Online PDF Printer is language-agnostic; as long as you can POST data
from your application, you can use our service.
Generating a PDF from Start to Finish
Create and Upload Document Templates
All PDFs derive from Microsoft Word documents (.doc and .docx)
that you upload to Online PDF Printer to use as templates. We decided
to do this because everybody has either OpenOffice or Word installed on
their computer. Customers appreciate the freedom to compose their own
documents and reports for the developer to use, and from the developer's
perspective, it saves a lot of time to let a word processor worry about
advanced formatting in your documents.Each template is then populated
with variable replacements, capitalized and enclosed by curly braces;
for instance, if you pass in a static variable called name, your
template would contain {NAME}, date would be {DATE} and
so on.
Multiple templates can also be combined into a single PDF download.
Sometimes you need to create a report that is of variable length and
variable content and the only way to achieve it is to create separate
templates for each section of the report, populate them individually with
data, then instruct Online PDF Printer to combine everything into a
single PDF.
How you construct your templates is a matter of personal preference; since
you're using a word processor to build the container for dynamic data, you
need to be aware of the limitations of word processors:
- Tables can be tricky.
You can quickly add a new row to a table in HTML without worrying about
page layout, since web pages are designed to be vertically-oriented and
scroll without page breaks.
Professional documents, however, usually incorporate finite spacing and
page breaks to output a clean, easily-read document.
Dealing with N-row sets of data in Online PDF Printer is easy,
you just have to decide how many rows of a given table should exist on a
single page and include that number of rows in the data POSTed to the
gateway.
If 10 rows can comfortably fit on a single page and you post 32 rows of
data to that template, four pages will be populated and combined in
order to accommodate the entire dataset.
(see example 2 for a demonstration of this)
We have also seen users create a space in their document for data to be
populated into, formatted that area to use a fixed-width font (like
Courier) and then POSTed a single variable of pre-formatted text data
that creates the illusion of a table, similar to how you'd format
tabular output in an application running in a text console.
Format Your Data
Online PDF Printer accepts two types of data:
- Static - a single array (row) of data that 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)
There are three basic data formats:
- A single-template document
options = array("api_key" => YOUR API KEY,
"template" => FILENAME OF WORD TEMPLATE,
"static_data" =>
array("field_1" => VALUE,
"field_2" => VALUE,
),
"dynamic_rows" =>
array(
array("row_field_1" => VALUE,
"row_field_2" => VALUE,
),
array("row_field_1" => VALUE,
"row_field_2" => VALUE,
),
);
"rows_per_page" => NUMBER OF DYNAMIC ROWS PER PAGE,
);
- A multi-template combination document
options = array("api_key" => YOUR API KEY,
"use_multiple_templates" => true,
"template" =>
array("FILENAME OF WORD TEMPLATE #1",
"FILENAME OF WORD TEMPLATE #2",
),
"static_data" =>
array("FILENAME OF WORD TEMPLATE #1" =>
array("field_1" => VALUE,
"field_2" => VALUE,
),
"FILENAME OF WORD TEMPLATE #2" =>
array("field_1" => VALUE,
"field_2" => VALUE,
),
),
"dynamic_rows" =>
array("FILENAME OF WORD TEMPLATE #1" =>
array(
array("row_field_1" => VALUE,
"row_field_2" => VALUE,
),
array("row_field_1" => VALUE,
"row_field_2" => VALUE,
),
),
"FILENAME OF WORD TEMPLATE #2" =>
array(
array("row_field_1" => VALUE,
"row_field_2" => VALUE,
),
array("row_field_1" => VALUE,
"row_field_2" => VALUE,
),
),
),
"rows_per_page" =>
array("FILENAME OF WORD TEMPLATE #1" => NUMBER OF DYNAMIC ROWS PER PAGE,
"FILENAME OF WORD TEMPLATE #2" => NUMBER OF DYNAMIC ROWS PER PAGE,
),
);
- A multi-template combination document, generated multiple
times with variable data, returned as a single document
options = array("api_key" => YOUR API KEY,
"use_multiple_templates" => true,
"combine_n_reports" => true,
"template" =>
array("FILENAME OF WORD TEMPLATE #1",
"FILENAME OF WORD TEMPLATE #2",
),
"static_data" =>
array(
// DATASET #1
array("FILENAME OF WORD TEMPLATE #1" =>
array("field_1" => VALUE,
"field_2" => VALUE,
),
"FILENAME OF WORD TEMPLATE #2" =>
array("field_1" => VALUE,
"field_2" => VALUE,
),
),
// DATASET #2
array("FILENAME OF WORD TEMPLATE #1" =>
array("field_1" => VALUE,
"field_2" => VALUE,
),
"FILENAME OF WORD TEMPLATE #2" =>
array("field_1" => VALUE,
"field_2" => VALUE,
),
),
),
"dynamic_rows" =>
array(
// DATASET #1
array("FILENAME OF WORD TEMPLATE #1" =>
array(
array("row_field_1" => VALUE,
"row_field_2" => VALUE,
),
array("row_field_1" => VALUE,
"row_field_2" => VALUE,
),
),
"FILENAME OF WORD TEMPLATE #2" =>
array(
array("row_field_1" => VALUE,
"row_field_2" => VALUE,
),
array("row_field_1" => VALUE,
"row_field_2" => VALUE,
),
),
),
// DATASET #2
array("FILENAME OF WORD TEMPLATE #1" =>
array(
array("row_field_1" => VALUE,
"row_field_2" => VALUE,
),
array("row_field_1" => VALUE,
"row_field_2" => VALUE,
),
),
"FILENAME OF WORD TEMPLATE #2" =>
array(
array("row_field_1" => VALUE,
"row_field_2" => VALUE,
),
array("row_field_1" => VALUE,
"row_field_2" => VALUE,
),
),
),
),
"rows_per_page" =>
array("FILENAME OF WORD TEMPLATE #1" => NUMBER OF DYNAMIC ROWS PER PAGE,
"FILENAME OF WORD TEMPLATE #2" => NUMBER OF DYNAMIC ROWS PER PAGE,
),
);
Special Data Types
Sometimes you'll need to populate certain types of form elements in your PDF
output, such as inserting checkboxes or radio buttons. Online PDF Printer
allows you to request special procedures when you format your data so that your
document ends up looking the way that you want it.
All special procedure requests are just strings that you use as data that's
POSTed to the gateway, prefixed by PROC::
| Type |
Example Data |
Description |
| Checkbox |
PROC::CHECKBOX(true) PROC::CHECKBOX(false) |
This creates a checkbox that is checked if true and unchecked if false. |
| Radio |
PROC::RADIO(true) PROC::RADIO(false) |
This creates a radio button that is selected if true and unselected if false. |
| Image |
PROC::IMAGE(filename) PROC::IMAGE(Base64 encoded JPEG data) |
This allows you to dynamically insert either a JPEG file uploaded to your account or a Base64 encoded stream of JPEG data directly from your application.
NOTE: There are some optional parameters that you can add to this special procedure to control various attributes of the imported image.
- DPI (int) - the resolution of the image. This is often 72, but high-res imagery will be at 300 DPI or higher
- WIDTH (float) - the desired image width in inches. The image is scaled by the PDF.
- HEIGHT (float) - the desired image height in inches. The image is scaled by the PDF.
To use any of these parameters, simply append them to your data string in any combination or order:
ex. PROC::IMAGE(logo.jpg)::DPI(300)::WIDTH(4.5)::HEIGHT(1.25)
|
Example:
$static_data = array("name" => "My name!",
"is_human" => "PROC::CHECKBOX(true)",
"is_child" => "PROC::CHECKBOX(false)",
"my_logo" => "PROC::IMAGE(logo.jpg)",
"secondary_logo" => "PROC::IMAGE(" . base64_encode(file_get_contents("./logo2.jpg")) . ")",
);
-
Available Options:
| |
Type / Possible Values |
Description |
| api_key |
String |
This is the API key assigned to your account when you signed up. |
| template |
String or Array |
This is either the filename of an uploaded Word template, or an
array of filenames of templates to be combined. |
| static_data |
Array |
This is either a one-dimensional array of variables to be
populated, or a multi-dimensional array of static data to be used in
multiple templates. |
| dynamic_rows |
Array |
This is either a one-dimensional array of variables to be
populated, or a multi-dimensional array of dynamic data to be used
in multiple templates. |
| rows_per_page |
Number or Array |
This is either the number of available dynamic rows in a single
template, or an array of available dynamic rows in multiple
templates. |
| break_on_new_page |
Boolean, true / false |
True by default, this forces an explicit page break between
pages when combining multiple templates or populating N pages. This
behavior sometimes is unwanted in certain types of documents, so it
can be toggled. |
| use_multiple_templates |
Boolean, true / false |
Specifies whether or not you're populating multiple templates
and combining them into a single document. |
| combine_n_reports |
Boolean, true / false |
Specifies whether or not you're generating a complete document
multiple times with variable datasets, then combining all document
output into a single document. This is most useful in creating a
date-range report but downloading the entire output as a single PDF. |
| test_mode |
Boolean, true / false |
If you print in test mode, your PDF output is watermarked but the generated PDF doesn't count against your number of allowed monthly prints. This is primarily used so you can make sure your documents print correctly before being put into production. |
POST to the Gateway
Different languages POST in different ways, but one of the easiest is
using cURL.
You just send the data to
http://print.onlinepdfprinter.com on port 80.
$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()
Manipulate the PDF Stream
The gateway returns binary PDF data. You can do whatever you want with
it.
Filenames are determined by your application after the PDF stream has been
received.
The most common response to this data is to force the browser to initiate a
download:
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()
|
|