Documentation
jump to:
    Main Function: phptopdf()         PDF Options         Legacy Functions


Quick Setup

As usual, if there are any questions / feedback / issues, please contact us.


function phptopdf()

This function generates a PDF based on the pdf options specified - see below.

The phpToPDF.php file needs to be included and have your API key inside.

The $pdf_options array is where you specify your PDF options. Each option should go on one line and each line should end with a comma (,) - except the last line - as you can see below.

Lastly, call phptopdf($pdf_options) and your PDF wil be generated.

<?php
    
require('phpToPDF.php');

    
//Set Your Options -- see documentation for all options
    
$pdf_options = array(
          
"source_type" => 'url',
          
"source" => 'http://google.com',
          
"action" => 'save');

    
//Code to generate PDF file from options above
    
phptopdf($pdf_options);
?>


Check out our Examples Page for ready to run samples.

PDF Options

•   source_type •   color •   header
•   source •   page_orientation •   footer
•   action •   page_size •   Include Page Numbers
•   save_directory •   margin  
•   file_name •   omit_images  
  •   encoding •   Force Page Breaks

source_type

Accepted Values:

'url' or 'html'

Default: 'url'

Description:

Specify the source type of the required 'source' option.
url = a URL (like http://google.com) will be passed.
html = some HTML code will be passed.

Example Syntax:

"source_type" => 'url'


source         [ required ]

Accepted Values:

Any URL starting with http:// or https:// or any HTML code starting with

<!doctype...><html...> or <html...> and ending with </html>

Description:

If the 'source_type' option is 'url', put a URL here (like http://google.com).
If the 'source_type' option is 'html', put some HTML here (must begin with HTML tags!).

Example Syntax:

"source" => 'http://google.com' or "source" => '<html>Hello World!</html>'


action

Accepted Values:

'save' or 'download' or 'view'

Default: 'view'

Description:

This specifies the action taken after PDF generation.
'save' (requires the 'save_directoty' option) will save the PDF to a directory on your server.
'download' will force the PDF to download in the user's browser.
'view' will force the PDF to open from the user's browser.

Example Syntax:

"action" => 'save'


save_directory

Accepted Values:

Any path on your server such as '/var/www/html/files/'.       Use '' for current directory.

Default: '' (current directory)

Note: This directory must have write permission (chmod 775 or 777)

Description:

This is the directory on your server where the PDF will be saved and stored

Example Syntax:

"save_directory" => '/pdfs'


file_name

Accepted Values:

Any valid file naming convention such as 'mypdf.pdf'

Default: (A random string).pdf

Description:

Sets the filename of the generated PDF

Example Syntax:

"file_name" => 'daily_report.pdf'


color

Accepted Values:

'color' or 'monochrome'

Default: 'color'

Description:

Should the PDF be in color or monochrome (Black & Grey & White)

Example Syntax:

"color" => 'monochrome'


page_orientation

Accepted Values:

'portrait' or 'landscape'

Default: 'portrait'

Description:

Set the Portrait (vertical) or Landscape (horizontal) page orientation

Example Syntax:

"page_orientation" => 'landscape'


page_size

Accepted Values:

'letter', 'legal', 'A0', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'B0', 'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B9', 'B10', 'ledger', 'tabloid'

You can also specify an exact width x height value in millimeters.

Default: 'letter'

Description:

Set the PDF page size (Letter, Legal, A4, A5, etc. OR a specific size 75mm x 240mm, etc.)

Example Syntax:

"page_size" => 'A5'

      or

"page_size" => '40x90'


margin

Accepted Values:

You specify an exact value in millimeters for the page margin: right, left, top, bottom.

array("right"=>"25","top"=>"120"),   array("right"=>"0","left"=>'0',"top"=>"0","bottom"=>"0"),   etc.

Default: array("right"=>"5","left"=>'5',"top"=>"10","bottom"=>"10")

(Top/Bottom = 10mm Left/Right = 5mm)

Description:

Set the margin of the PDF in millimeters

Example Syntax:

"margin"=>array("right"=>"15","left"=>'15',"top"=>"30","bottom"=>"60")


omit_images

Accepted Values:

'no' or 'yes'

Default: 'no'

Description:

Generate PDF without any images. If this is not specified, images will be included

Example Syntax:

"omit_images" => 'yes'


encoding

Accepted Values:

'UTF-8', 'ISO-8859-1', 'JIS', 'Windows-1251', etc. More Here

Default: 'UTF-8'

Description:

Changes the character encoding to the specified encoding. If text output is not appearing as desired, change the character encoding to support your text.

Example Syntax:

"encoding" => 'ISO-8859-1'


header

Accepted Values:

Any text or html snippet

Default: none (header not shown)

Description:

This header will appear on the top of all pages.
By default, it aligns center but with the recommended code snippet below, you can align it anywhere or have left, center & right text all at the same time.
You can also include page numbers. See here for details on including page numbers.

Check out this header example.

Example Syntax:

<?php
//HEADER VARIABLE HOLDING -- LEFT, CENTER, RIGHT TEXT
$my_html_header="
<div style=\"display:block;\">
  <div style=\"float:left; width:33%; text-align:left;\">
         Left Header Text
  </div>
  <div style=\"float:left; width:33%; text-align:center;\">
         Center Header Text
  </div>
  <div style=\"float:left; width:33%; text-align:right;\">
       Right Header Text
   </div>
   <br style=\"clear:left;\"/>
</div>"
;
?>
              
              

"header" => $my_html_header


footer

Accepted Values:

Any text or html snippet

Default: none (footer not shown)

Description:

This footer will appear on the top of all pages.
By default, it aligns center but with the recommended code snippet below, you can align it anywhere or have left, center & right text all at the same time.
You can also include page numbers. See here for details on including page numbers.

Check out this footer example.

Example Syntax:

<?php
//FOOTER VARIABLE HOLDING -- LEFT, CENTER, RIGHT TEXT
$my_html_footer="
<div style=\"display:block;\">
  <div style=\"float:left; width:33%; text-align:left;\">
         Left Footer Text
  </div>
  <div style=\"float:left; width:33%; text-align:center;\">
         Center Footer Text
  </div>
  <div style=\"float:left; width:33%; text-align:right;\">
       Right Footer Text
   </div>
   <br style=\"clear:left;\"/>
</div>"
;
?>
              
              

"footer" => $my_html_footer




Include Page Numbers

phpToPDF Header/Footer Page Number Variables:

phptopdf_on_page_number - The current page's page number.

phptopdf_pages_total - Total number of pages in the PDF.

Description:

Do not use a $ for these variables.
These are two variables you can include anywhere in your header or footer. When the PDF is generated, those variables will be translated accordingly.

Check out the examples page for detailed header/footer with page numbers examples.

Example Syntax:

"footer" => "Page phptopdf_on_page_number of phptopdf_pages_total"

In this example, assume we are looking at page 3 of a 7 page PDF, the footer will show

Page 3 of 7




Force Page Breaks

How To Force Page Breaks:

Forcing page breaks is something that is done by simply calling a DIV with the with the class set as phpToPDF-page-break in your HTML source. During PDF generation we create page breaks wherever we see the phpToPDF-page-break DIV class.

View the source code below and View This Page Break Example.

<?php
//VARIABLE HOLDING OUR SOURCE -- THIS CREATES A 3 PAGE PDF
$my_html="<html>
<head>
</head>
<body>
<h1>Page Title</h1>
   <!-- content block (Page 1) -->
   <h1> This Is Page 1 </h1>
<div class=\"phpToPDF-page-break\"></div>
   <!-- content block (Page 1) -->
   <h1> This Is Page 2 </h1>
<div class=\"phpToPDF-page-break\"></div>
   <!-- content block (Page 1) -->
   <h1> This Is Page 3 </h1>
</body>
</html>"
;








The phptopdf() function - above - replaces the two functions below.   Both phptopdf_url() and phptopdf_html() still work as they always did, but we urge you to change to the friendlier phptopdf function.

function phptopdf_url()             Legacy - limited support

Generate PDF from a specified URL

Syntax:

phptopdf_url($url, $destination_dir, $destination_filename)

Description

  • $url :- URL to convert into a PDF
  • $destination_dir :- Destination directory where the generated PDF is to be stored
  • $filename :- Name of the generated PDF file

 

function phptopdf_html()             Legacy - limited support

Generate a PDF from HTML

Syntax:

phptopdf_html($html, $destination_dir, $destination_filename)

Description

  • $html :- Variable containing the HTML
  • $destination_dir :- Destination directory where the generated PDF is to be stored
  • $filename:- Name of the generated PDF file

 

Back to Top