Legacy Pdfcrowd API v1 for PHP

This is the documentation of the PHP client library for the legacy Pdfcrowd API v1. We strongly recommend the new improved Pdfcrowd API v2 for new integrations.

Installation

Install the Pdfcrowd API client library for PHP.

Getting Started

This code converts a web page and sends the generated PDF as an HTTP response (in the following examples, do not forget to replace "username" and "apikey" with your username and API key)

<?php
require 'pdfcrowd.php';

try
{   
    // create an API client instance
    $client = new Pdfcrowd("your_username", "your_apikey");

    // convert a web page and store the generated PDF into a $pdf variable
    $pdf = $client->convertURI('http://www.google.com/');

    // set HTTP response headers
    header("Content-Type: application/pdf");
    header("Cache-Control: max-age=0");
    header("Accept-Ranges: none");
    header("Content-Disposition: attachment; filename=\"google_com.pdf\"");

    // send the generated PDF 
    echo $pdf;
}
catch(PdfcrowdException $why)
{
    echo "Pdfcrowd Error: " . $why;
}
?>

You can also convert raw HTML code, just use the convertHtml() method instead of convertURI():

$pdf = $client->convertHtml("<head></head><body>My HTML Layout</body>");

The API lets you also convert a local HTML file:

$pdf = $client->convertFile("/path/to/MyLayout.html");

You can save the generated PDF to a file:

$out_file = fopen("document.pdf", "wb");
$client->convertHtml("<head></head><body>My HTML Layout</body>", $out_file);
fclose($out_file);

Error Handling

try {
    // ..
    // call the API
} catch(PdfcrowdException $why) {
    // $why->getMessage() returns an error message
}

API Reference

class Pdfcrowd

Provides access to the Pdfcrowd API from your PHP applications.

Constructor

function __construct($username, $apikey)
Arguments are your username at Pdfcrowd and apikey which can be found in your account.

Conversion

function convertHtml($html, $outstream=null)
Converts the html string to PDF and writes the result to outstream. outstream is a handle created using fopen(). If outstream is not provided then the return value is a string containing the created PDF.
function convertFile($fpath, $outstream=null)
Converts a local file fpath to PDF and writes the result to outstream. The file can be either an HTML document or a .zip, .tar.gz., or .tar.bz2 archive which can contain external resources such as images, stylesheets, etc.
outstream is a handle created using fopen(). If outstream is not provided then the return value is a string containing the created PDF.
function convertURI($url, $outstream=null)
Converts a web page at url to PDF and writes the result to outstream. outstream is a handle created using fopen(). If outstream is not provided then the return value is a string containing the created PDF.

Page Setup

function setPageWidth($value)
Sets PDF page width in units.
function setPageHeight($value)
Sets PDF page height in units. Use -1 for a single page PDF.
function setPageMargins($top, $right, $bottom, $left)
Sets PDF page margins in units.
function setHorizontalMargin($value)
Deprecated. Use setPageMargins instead.
function setVerticalMargin($value)
Deprecated. Use setPageMargins instead.

Header and Footer

function setFooterHtml($html)
Places the specified html code inside the page footer. The following variables are expanded:
  • %u - URL to convert.
  • %p - The current page number.
  • %n - Total number of pages.
function setFooterUrl($url)
Loads HTML code from the specified url and places it inside the page footer. See setFooterHtml for the list of variables that are expanded.
function setHeaderHtml($html)
Places the specified html code inside the page header. See setFooterHtml for the list of variables that are expanded.
function setHeaderUrl($url)
Loads HTML code from the specified url and places it inside the page header. See setFooterHtml for the list of variables that are expanded.
function setHeaderFooterPageExcludeList($exclist)
exclist is a comma seperated list of physical page numbers on which the header a footer are not printed. Negative numbers count backwards from the last page: -1 is the last page, -2 is the last but one page, and so on.
Example: "1,-1" will not print the header and footer on the first and the last page.
function setPageNumberingOffset($offset)
An offset between physical and logical page numbers. The default value is 0.
Example: if set to "1" then the page numbering will start with 1 on the second page.

HTML options

function enableImages($value)
Set value to False to disable printing images to the PDF. The default is True
function enableBackgrounds($value)
Set value to False to disable printing backgrounds to the PDF. The default is True
function setHtmlZoom($value)
Set HTML zoom in percents. It determines the precision used for rendering of the HTML content. Despite its name, it does not zoom the HTML content. Higher values can improve glyph positioning and can lead to overall better visual appearance of generated PDF .The default value is 200. See also setPdfScalingFactor().
function enableJavaScript($value)
Set value to False to disable JavaScript in web pages. The default is True.
function enableHyperlinks($value)
Set value to False to disable hyperlinks in the PDF. The default is True.
function setDefaultTextEncoding($value)
value is the text encoding used when none is specified in a web page. The default is utf-8.
function usePrintMedia($value)
If value is True then the print CSS media type is used (if available).

PDF options

function setEncrypted($value)
If value is set to True then the PDF is encrypted. This prevents search engines from indexing the document. The default is False.
function setUserPassword($pwd)
Protects the PDF with a user password. When a PDF has a user password, it must be supplied in order to view the document and to perform operations allowed by the access permissions. At most 32 characters.
function setOwnerPassword($pwd)
Protects the PDF with an owner password. Supplying an owner password grants unlimited access to the PDF including changing the passwords and access permissions. At most 32 characters.
function setNoPrint($value)
Set value to True disables printing the generated PDF. The default is False.
function setNoModify($value)
Set value to True to disable modifying the PDF. The default is False.
function setNoCopy($value)
Set value to True to disable extracting text and graphics from the PDF. The default is False.
function setPageLayout($value)
Specifies the initial page layout when the PDF is opened in a viewer.
  • SINGLE_PAGE
  • CONTINUOUS
  • CONTINUOUS_FACING
function setPageMode($value)
Specifies the appearance of the PDF when opened.
  • FULLSCREEN - Full-screen mode.
function setInitialPdfZoomType($value)
value specifies the appearance of the PDF when opened.
  • FIT_WIDTH
  • FIT_HEIGHT
  • FIT_PAGE
function setInitialPdfExactZoom($value)
value specifies the initial page zoom of the PDF when opened.
function setPdfScalingFactor($value)
The scaling factor used to convert between HTML and PDF. The default value is 1.0.
function setPageBackgroundColor($value)
The page background color in RRGGBB hexadecimal format.
function setTransparentBackground($value)
Does not print the body background. Requires the following CSS rule to be declared:
body {background-color:rgba(255,255,255,0.0);}
function setAuthor($author)
Sets the author field in the created PDF.

Watermark

function setWatermark($url, $offset_x=0, $offset_y=0)
url is a public absolute URL of the watermark image (must start either with http:// or https://). The supported formats are PNG and JPEG. offset_x and offset_y is the watermark offset in units. The default offset is (0,0).
function setWatermarkRotation($angle)
Rotates the watermark by angle degrees.
function setWatermarkInBackground($value)
When value is set to True then the watermark is be placed in the background. By default, the watermark is placed in the foreground.

Miscellaneous

function useSSL($use_ssl)
Set to True to call the API over a secure connection. The default is False.
function numTokens()
Returns the number of available conversion credits in your account.
function setMaxPages($npages)
Prints at most npages pages.
function setFailOnNon200($value)
If value is True then the conversion will fail when the source URI returns 4xx or 5xx HTTP status code. The default is False.

class PdfcrowdException

Derived from Exception. It is thrown when an error occurs. Use getMessage() method to get the error message. getCode() returns the HTTP status code.

Units

Page dimensions and margins can be specified in inches (in), millimeters (mm), centimeters (cm), or points (pt). If no units are specified, points are assumed. Examples: "210mm", "8.5in".

Important: This document is for the legacy Pdfcrowd API v1. The documentation for the new API v2 can be found here.