Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The code snippet example will generate a simple pdf from the PDF serviceallow you to create a PDF file from HTML code that you pass to the PDF service. You will then be returned either a download link, or optionally the PDF file contents in the API response.

Code Snippet Example:

...


Code Block
languagephp
themeRDark
titleBuild PDF
//toImport import the pdf service classes
$t->runInteraction("Pdf.class.php");
//import config class which contains keys
$t->runInteraction("Config.class.php");

//setSet header/footer keys, footer, and page content HTML Code
//HTML5 and CSS3 is supported
$header_html = "{_BLINK_PAGE_NO_}/{_BLINK_PAGES_}<br><hr>";
$footer_html = "<hr><br>{_BLINK_PAGE_NO_}/{_BLINK_PAGES_}";
$body = "<table><td>PDF Example: " . date("c") . "</td></tr></table>";

$content = array(
   "page" => array(
       "orientation" => "Portrait",
       "size" => "A4",
       "margins" => array(
           "top" => "5mm",
           "right" => "1cm",
           "bottom" => "5mm",
           "left" => "1cm"
       )
   ),
   "header" => array(
       "height" => "1in2cm",
       "html" => $header_html
   ),
   "footer" => array(
       "height" => "1in2cm",
       "html" => $footer_thmlhtml
   ),
   "body" => array(
       "html" => $body
   )
);


echo "About to build<BR>";

//create object
//Call the PDF Service
$pdf = new PDFBuilder(Config::$PDF);
//build
pdf, if base64 is required
$result = $pdf->build($content);

echo "<pre>";
print_r($result);
echo "</pre>";

/*Setting $includeBase64Content tryto {
print_r($result);
 } catch (Exception $e) {
      print_r($result);
 }
*/

//Following code sends an email to your email address with pdf as attachment
$t->addStringAttachment(file_get_contents($result["url"]), "Report.pdf", "base64");
$from = "bmp@blinkmobile.com.au";
$to = "YOUR ADDRESS EMAIL";
$subject = "[BlinkMobile] PDF Demo";
$body = "PDF Attached.";
$t->email($to, $subject, $body, $from);

return "Done<BR>";


/**
* $result = array(
*  "id" => "<UNIQUE OBJECT ID>",
*  "url" => "http://url.to.pdf/valid.for.3.minutes",
*  "content" => "base64 pdf content"
* );
**/true will include the PDF file content in the response object
//Set to false to access the PDF file from a time limited URL instead
$includeBase64Content = true;
$result = $pdf->build($content, $includeBase64Content);


 

Explanation:

The first two "runInteraction" lines will simply "include" the class files you already uploaded. Make sure your have used the correct interaction names. You will need to put both of these lines on any page, or interaction, that you will be calling the class.

In the code example above, the following functions are used.

The function calls the PDF Service with your access credentials set up in the Config.class.php file and sends your PDF content to be created.

Code Block
languagephp
titlenew PDFBuilder
$pdf = new PDFBuilder(Config::$PDF);

Optional: You can include BMP mADL code to send you the results of the generated PDF via email. 

...

...

$t->email($to, $subject, $body, $from);

See more information on $t->email

$header_html (

...

string)

The header array can contain any HTML code or CSS styling you want in the header section will be displayed at the top of each page of your PDF document. In the example above, we're including the two built in variables . You can use HTML code and styles to display your header.

Additionally, you can also use the special keywords for dynamic values:

{_BLINK_PAGE_NO_} /will display the current page in the PDF document.

{_BLINK_PAGES_} which will display the page number and total page numberstotal number of pages in the PDF document.

...

 

 

...

html : (string) containing your header html code

...

Info

...

height : (array) use the same parameters as page:margin

...

Note: If you are using images in your header or footer, you will need to also place the same image in the body of your document, with the style set to display:none;. This is a current restriction with the library which we are looking at ways to get around.

 

 

$footer_html (

...

string)

The footer array can contain any HTML code or CSS styling you want in the footer section will be displayed at the bottom of each page of your PDF document.  In the example above, we're including the two built in variables You can use HTML code and styles to display your footer.

Additionally, you can also use the special keywords for dynamic values:

{_BLINK_PAGE_NO_} /will display the current page in the PDF document.

{_BLINK_PAGES_} which will display the page number and total page numbers.

footerAn array containing your header styling and html code 
 

height : (array) use the same parameters as page:margin

 "height" => "1in" (default)
 

html : (string) containing your footer html code

 "html" => $footer_thml

$body (array)

The body array can contain any HTML code or CSS styling you want in the body of your PDF document.

bodyAn array containing your header styling and html code 
 

html : (string) containing your footer html code

     "html" => $body

total number of pages in the PDF document.


 

Info
Note: If you are using images in your header or footer, you will need to also place the same image in the body of your document, with the style set to display:none;. This is a current restriction with the library which we are looking at ways to get around.

 

 

$body (string)

 

The body of the PDF document is what will be displayed in the main page area of your PDF document. This supports HTML5 and CSS3, and offers very accurate rendering for even some of the more complex HTML objects.

 

 

Page Layout

$content (array)

The content array contains tall the parameters required to produce a PDF document, including layout options and content.

...

 

Code Block
languagejs
themeRDark
titleDefault Page Parametres
 "page": {
    "orientation": "P",
    "size": "A4",
    "margins": {
      "top": "5mm",
      "right": "1cm",
      "bottom": "5mm",
      "left": "1cm"
    }


$content['page

...

size : (string) page size

...

  • A4 (default)

...

'] (array)

The $content['page'] array allows you to set your PDF page layout options:

orientation: P | L

size: A3 | A4 (default) | A5 | Legal | Letter | Tabloid

margin (array): top, right, bottom

...

  • "top": "5mm"
  • "right": "1cm"
  • "bottom": "5mm"
  • "left": "1cm"

Response:

After running this method, you will receive a response object that will simply return the data you logged. You can use this to double check values if you wish, but this is optional.

...

, left keys accept a number followed by the unit px (default) | mm | cm | in


 

Response:

After running this method, you will receive a response object array:

Code Block
languagephp
themeRDark
titleResult Array
$result = array(
   "id" => "<UNIQUE OBJECT ID>",
   "url" => "http://url.to.pdf/valid.for.3.minutes",
   "content" => "base64 pdf content"
}


id (string)

This is a unique ID string for this PDF document.

url (string)

This is a private signed URL to the PDF document that will be accessible for three minutes. Depending on your use case, you may attach this to an email or offer it for download. Keep in mind that this link will expire after three minutes, so you will need to consider what to do with the document longer term.

content (base64 string)

If you pass the variable $includeBase64Content = true when you call the service,  the key "content" will also be included in the response array. The value of this key will be file contents of the PDF document, Base64 encoded.