Documentation

Get Token Key #

Token key is a secret string with 64 characters used to access our service. You need to attach your token in every request you make to our service.

Steps to generate token key:

  1. If you have account with us, go to step 2 otherwise sign-up with us. An email with title "Welcome to ChromeHeadless.io" will be sent to you in few minutes after your sign up.
  2. Use account credential in welcome email to log in our system.
  3. Go to tokens management page
  4. Hit Generate button to generate token key.

REST End Point #

Our end point of PDF export service is located at:

https://service.chromeheadless.io/api/export

PHP Client #

If you are using PHP, for your convenience in connecting to our service you should install the PHP Client.

Installation #

The PHP Client can be installed through composer

composer require chromeheadlessio/php-client

Example #

<?php
//Use PHP Client Library
require_once "vendor/autoload.php";

//Create ChromeHeadless service with your token key specified
$service = new \chromeheadlessio\Service("my-token-key");

//Get PDF generated from html content and push it to browser
$service->export([
    "html"=>"Hello world!"
])->pdf([
    "format"=>"A4",
    "orientation"=>"portrait"
])->sendToBrowser("helloworld.pdf");

Exporting content #

The export() method belongs the service class. It receives an array as parameter defining what you need to export. Below are list of properties:

NameTypeDefaultDescription
htmlstringThe html you want to convert
httpHoststring"localhost"If set the httpHost and the baseUrl will be used to replace the resource link within html
baseUrlstringThe location which the html file should be in virtually
urlstringIf html is not set and url is set instead then the url will used by php client
timeoutnumber30Maximum navigation time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout.
waitUntilstring"load"When to consider navigation succeeded. Other options are "domcontentloaded" page finished when all DOM is loaded; "networkidle0" page finished when there are no more than 0 network connections for at least 500 ms; "networkidle2" page finished when there are no more than 2 network connections for at least 500 ms.

Export to PDF #

The pdf() method will help to generate pdf file. It takes an array as parameter defining options for your PDF. Below are available options.

NameTypeDefaultDescription
scalenumber1Scale of the webpage rendering. Defaults to 1. Scale amount must be between 0.1 and 2
displayHeaderFooterboolfalseDisplay header and footer.
headerTemplatestringHTML template for the print header. Should be valid HTML markup with following classes used to inject printing values into them: pageNumber current page number; totalPages total pages in the document;
footerTemplatestringHTML template for the print footer. Should use the same format as the headerTemplate
printBackgroundboolfalsePrint background graphics.
landscapeboolfalsePaper orientation.
pageRangesstringPaper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty string, which means print all pages.
formatstringPaper format. If set, takes priority over width or height options. Defaults to 'Letter'.
widthstring/numberPaper width, accepts values labeled with units.
heightstring/numberPaper height, accepts values labeled with units.
marginobjectPaper margins, defaults to none. It has 4 sub properties: top, right, bottom, left which can take number or string with units

Example:

$service->export(...)->pdf([
    "scale"=>1,
    "format"=>"A4",
    "landscape"=>true
])->sendToBrowser("myfile.pdf");

Export to PNG #

The png() help to generate PNG file. It take an array as parameter defining options for your PNG. Below are list of properties:

NameTypeDefaultDescription
fullPageboolfalseWhen true, takes a screenshot of the full scrollable page.
clipobjectAn object which specifies clipping region of the page. Should have the following fields: x is the x-coordinate of top-left corner of clip area, y is y-coordinate of top-left corner of clip area, width is the width of clipping area and height is the height of clipping area.
omitBackgroundboolfalseHides default white background and allows capturing screenshots with transparency.
encodingstring"binary"The encoding of the image, can be either base64 or binary

Example:

$service->export(...)->png([
    "clip"=>[
        "x"=>100,
        "y"=>100,
        "width"=>500,
        "height"=>1000,
    ]
])->sendToBrowser("myfile.png");

Export to JPG #

The jpg() help to generate JPG file. It take an array as parameter defining options for your JPG. Below are list of properties:

NameTypeDefaultDescription
qualitynumberThe quality of the image, between 0-100.
fullPageboolfalseWhen true, takes a screenshot of the full scrollable page.
clipobjectAn object which specifies clipping region of the page. Should have the following fields: x is the x-coordinate of top-left corner of clip area, y is y-coordinate of top-left corner of clip area, width is the width of clipping area and height is the height of clipping area.
omitBackgroundboolfalseHides default white background and allows capturing screenshots with transparency.
encodingstring"binary"The encoding of the image, can be either base64 or binary

Example:

$service->export(...)->jpg([
    "quality"=>80
    "clip"=>[
        "x"=>100,
        "y"=>100,
        "width"=>500,
        "height"=>1000,
    ]
])->sendToBrowser("myfile.jpg");

Getting result #

In all above examples we use method sendToBrowser() to send the file to browser for user to download. Here are all options:

MethodReturnDescription
sendToBrowser($filename, $inlineOrAttachment)Send file to client browser to open on browser or download as attachment. Default value is "attachment"
toString()stringReturn filename as string
toBase64()stringReturn content of file in base64
save($path)Save the file to specific location

Examples:

$service->export(...)->jpg([
    "quality"=>80
    "clip"=>[
        "x"=>100,
        "y"=>100,
        "width"=>500,
        "height"=>1000,
    ]
])->save("../img/myfile.jpg");

NodeJS Client #

[Coming soon...]

top