Recommend this page to a friend! |
Classes of Christian Vigh | ApiLayer API Encapsulation | README.PDFLAYER.md | Download |
|
DownloadINTRODUCTIONThe PdfLayer class encapsulates the PdfLayer api from ApiLayer. Using the PdfLayer object is pretty simple. Just instantiate an object, define the required properties, and call one of the PdfLayer public methods ; the following example will convert HTML contents from http://www.google.com and return PDF data in the $pdf variable (if an error occurs, an ApiLayerException exception will be thrown) :
The ConvertPage() method will use either the specified url, or the one specified in the Url property so you could rewrite this as :
Converted pages are cached as much as possible to minimize useless http requests ; however, you may want a new capture to be generated for the same url. In this case, use the Force property for that :
The PdfLayer API allows you to specify a secret key that you can define and activate in your dashboard ; Once activated, it must be specified in all your API requests (the secret_key API parameter is then the md5 hash of the requested url catenated with your dashboard's secret key). The PdfLayer class relieves you from having to calculate this final secret key and automatically generate it for you before issuing the API query ; simply specify in the class constructor the value of your dashboard's secret key as in the following example :
and the PdfLayer class will handle all the details for you. USEFUL LINKSYou can find the documentation on the ScreenshotLayer API from ApiLayers here : https://pdflayer.com/documentation Once you have successfully created a new account using your email address and a password, you will be able to see your dashboard information here : https://pdflayer.com/dashboard You will be able to the specify an access key different from the default one, or specify and activate a secret keyword. PDFLAYER CLASS CONSTRUCTORThe PdfLayer class constructor has the following syntax :
The parameters are the following :
PDFAYER CLASS PROPERTIESIn addition to the standard properties exposed by the base ApiLayer class, the following properties are available and mimic their counterpart in the screenshotlayer api ; have a look at the official documentation on https://pdflayer.com/documentation. Note that many properties have aliases. accept\_lang or accept\_language or AcceptLanguage (string)Sends the specified Accept-Language http header. Useful for sites that accept several user language translations. author or Author (string)Allows to set the document author, visible in the document properties when using Acrobat Reader. auth_user or AuthUser or AuthenticationUser (string)auth_password or AuthPassword or AuthenticationPassword (string)User and password required to connect to the website before being to view the page html contents. These scheme uses the basic http authentication mechanism. creator or Creator or application or Application (string)Allows to specify the name of the application that generated the pdf document. This information is visible in the document properties dialog box when using Acrobat Reader. css\_url or CssUrl (string)Url of a style sheet that will be injected to style html contents. custom\_unit or CustomUnit (keyword)Specifies the unit to be used for all other properties that express a quantity, such as margin\_left, margin\_right, page\_width, etc. The following units can be specified :
delay or Delay (integer)Specifies the maximum delay, in milliseconds, before the html contents can be considered to be completely loaded. document\_html or DocumentHtml (string)Specifies direct html contents to be converted to pdf. Either the document\_url or document\_html property needs to be specified. Note that this parameter cannot be specified as a GET parameter, but must be used as a POST parameter instead. If this property is assigned, the PdfLayer class will automatically issue a POST request to the Apilayer API. document\_name or DocumentName (string)By default, PDF documents generated by the pdflayer API are named pdflayer.pdf. Using the API's document_name parameter you can specify a custom name for your final PDF document. This information is mainly used when downloading generated documents. document\_url or DocumentUrl or url or UrlUrl of the web page to be converted into PDF format. Either the document\_url or document\_html property needs to be specified. encryption or Encryption (keyword)There are two encryption levels available for PDFs generated by the pdflayer API: 40-bit and 128-bit. In order to activate encryption, set the API's encryption parameter to 40 or 128. footer\_align or FooterAlign (keyword)Specifies alignment for footer text. This can take the values left, right or center. footer\_html or FooterHtml (string)Specifies direct html contents to be used for the generated pdf page header. Note that this parameter cannot be specified as a GET parameter, but must be used as a POST parameter instead. If this property is assigned, the PdfLayer class will automatically issue a POST request to the Apilayer API. footer\_text or FooterText (string)Specifies the footer text. footer\_spacing or FooterSpacing (integer)Specifies the spacing between the bottom of the page and the start of footer contents. The units used are give by the custom\_unit parameter, which defaults to px (pixels). footer\_url or FooterUrl (string)Specifies an url which contains html contents to be inserted into the footer part of a page. force or Force (boolean)Set to "1" (or true) if you want the pdf contents to be regenerated. forms or Forms (boolean)If form data is present in the html contents, then a pdf form will be generated. grayscale or GrayScale (boolean)Converts color information to grayscale. header\_align or HeaderAlign (keyword)Specifies alignment for header text. This can take the values left, right or center. header\_html or HeaderHtml (string)Specifies direct html contents to be used for the generated pdf page footer. Note that this parameter cannot be specified as a GET parameter, but must be used as a POST parameter instead. If this property is assigned, the PdfLayer class will automatically issue a POST request to the Apilayer API. header\_text or HeaderText (string)Specifies the header text. header\_spacing or HeaderSpacing (integer)Specifies the spacing between the bottom of the header and the start of page contents. The units used are give by the custom\_unit parameter, which defaults to px (pixels). header\_url or HeaderUrl (string)Specifies an url which contains html contents to be inserted into the header part of a page. inline or Inline (boolean)By default, accessing a pdflayer API request URL in a browser will trigger the download of the generated PDF (attachment behaviour). By setting the API's inline parameter to 1 the API will be requested to display the PDF in the browser instead. margin\_bottom or MarginBottom (integer)margin\_left or MarginLeft (integer)margin\_right or MarginRight (integer)margin\_top or MarginTop (integer)Specifies the bottom, left, right and top margins to be used for the document page contents. The units used are given by the custom\_unit parameter, which is px (pixels) by default. no\_background or NoBackground (boolean)Remove background images when converting html to pdf. no\_copy or NoCopy (boolean)Clipboard copy will be disabled in the generated pdf file. no\_hyperlinks or NoHyperlinks (boolean)Do not include hyperlinks in the captured html contents. no\_images or NoImages (boolean)Do not include images in the captured html contents. no\_javascript or NoJavascript (boolean)Do no process javascript when reading html contents. no\_modify or NoModify (boolean)User will not be authorized to modify the pdf contents. no\_print or NoPrint (boolean)Is set to true, printing will be disabled in the generated pdf file. owner\_password or OwnerPassword (string)Password to be specified for being able to modify the pdf file contents. page\_height or PageHeight (integer)Specifies a page height that overrides the default page size parameter. Note that the page\_width parameter must also be specified in this case. page\_size or PageSize (keyword)Allows to specify a page format that can be :
The class constants PAGE\_SIZE\_* provide an alias for these keywords. page\_width or PageWidth (integer)Specifies a page width that overrides the default page size parameter. Note that the page\_height parameter must also be specified in this case. secret_key or SecretKey (string)If you have activated your secret key in the pdflayer api, then you will have to set the SecretPassword property to that key. The SecretKey readonly property will return your own secret key for the given url when used as a query parameter ; this secret\_key url parameter is the md5 hash of the requested url catenated with your secret key. subject or Subject (string)Allows to set the document subject, visible in the document properties when using Acrobat Reader. test or Test or sandbox or Sandbox (boolean)Enables sandbox mode. This allows you for testing document conversion even if you ran out of queries authorized by your current subscription plan. Note that in this case the generated pdf documents will have the word "Sample" printed in red in the page background. text_encoding or TextEncoding (string)Specifies the text encoding to be used when retrieving an url contents. The default is UTF-8. title or Title (string)Allows to set the document title, visible in the document properties when using Acrobat Reader. ttl or Ttl or TTL (integer)Specifies the maximum time in seconds where a generated pdf document will be kept in the cache. This can range from 300 (5 minutes) to 2592000 (30 days). user\_password or UserPassword (string)Password to be specified for being able to view the pdf file contents. use\_print\_media or UsePrintMedia (boolean)Takes into account @media css instructions when generating the pdf contents. user\_agent or UserAgentSpecifies a custom user agent string. Some standard user agent strings are available as class constants starting with USER\_AGENT\_\*. viewport or Viewport (viewport dimensions)A viewport dimension for the generated captures, which has the form :
where width and height represent the dimension. The default is : 1440x900. watermark\_in\_background or WatermarkInBackground (boolean)By default, watermarks are placed in the foreground of the document. Set this parameter to true to put the watermark behind the page contents. watermark\_offset\_x or WatermakOffsetX (integer)watermark\_offset\_y or WatermakOffsetY (integer)Specifies the X and Y position of the upper-left corner of the watermark. The default is (0,0). watermark\_opacity or WatermarkOpacity (integer) ##Specifies the watermark opacity (0..100). The default is 20 (20%). zoom or Zoom (integer)Specifies a zoom factor for the captured html contents. The value can range from 0 to 50, 0 meaning "no zoom". Note that after zooming, the converted html contents will be truncated if they do not fit on the page. PDFLAYER CLASS METHODSThe following methods are implemented by the PdfLayer class : public function ConvertPage ( $url = false )Converts the html contents found at the specified url to pdf contents. The method returns the generated pdf file data. If the $url parameter is not specified, then the contents of the Url property will be used. public function ConvertPages ( $url_list, $output_directory, $prefix = 'capture.' )Captures a set of urls specified in the $url_list array and saves them in the specified $output\_directory directory, in PDF format. Filenames are generated using the specified $prefix prefix, followed by a sequential number and the '.pdf' file extension. Existing files with the same name will be overwritten. Note that the ApiLayer Pdf API does not provide any means of performing batch captures ; this is just a feature of the PdfLayer class, and should be used with care since it does not guarantee faster captures. However, this allows you to perform batch conversions using the same parameters for every conversion. public function ConvertHtml ( $contents = false )Converts the specified html contents to pdf contents. The method returns the generated pdf file data. If the $contents parameter is not specified, then the contents of the DocumentHtml property will be used. public function DownloadPage ( $url = false, $filename = false )This method can be used to force the download of a captured url. The $filename parameter, when specified, indicates the filename to be downloaded. The default is built from the url domain name with file path. Dots and slashes will be replaced with underlines. |