PHP Tutorial: convert a webpage into a PDF file

Source: Internet
Author: User
PDF files are the most common document format. If a website provides a PDF file for saving a webpage, it will be very convenient for many users: if it is very busy at a time, you can save it as a PDF file, later. As a developer or webmaster, you can push website content to users using PDF documents as the carrier. Of course, you can also aggregate data to

PDF files are the most common document format. If a website provides a PDF file for saving a webpage, it will be very convenient for many users: if it is very busy at a time, you can save it as a PDF file, later. As a developer or webmaster, you can push website content to users using PDF documents as the carrier. Of course, you can also aggregate data to

PDF files are the most common document format. If a website provides a PDF file for saving a webpage, it will be very convenient for many users: if it is very busy at a time, you can save it as a PDF file, later. As a developer or webmaster, you can push website content to users using PDF documents as a carrier. Of course, you can also aggregate data to a page and generate an e-book to make the data more aggregated. Here, we need to use the HTML to PDF Online API, an open-source tool from javascrowd.com. Before the official start, we need an API and a user name. You can register at javascrowd.com and download javascrowd. php to be used.

Reference javascrowd. php and instantiate the class
 ConvertURI ('HTTP: // www.itluren.com /');?? // Set the response header ?? // Content type ?? Header ("Content-Type :? Application/pdf ");?? // Cache ?? Header ("Cache-Control :? No-cache ");?? // Access range ?? Header ("Accept-Ranges :? None ");?? // Content attributes ?? Header ("Content-Disposition :? Attachment ;? Filename = \ "itluren_com1 \"");?? // Output the generated PDF file ?? Echo? $ Pdf ;?? // If an exception is returned? Save it to the $ why variable ??} Catch (inclucrowdexception? $ Why ){?? // Output error ?? Echo? "Pdfcrowd? Error :? "?.? $ Why ;??} ???> ??
Convert HTML code to PDF document

We can also directly convert HTML code into a pdf file: $ pdf? =? $ Client-> convertURI ('HTTP: // www.itluren.com /');? Change to $ pdf = $ client-> convertHtml ("Itluren");

Convert a local HTML file to a PDF file
$pdf=$client->convertFile("/path/to/itluren.html");??
Save the converted PDF file to your local device.
// Try to write it into a model to open itlurenode? If this file does not exist, create ?? $ Out_file = fopen ("itlurenode", "wb ");?? // Convert HTML code? And write it into the file ?? $ Client-> convertHtml ("itluren ",? $ Out_file );?? // Close the file ?? Fclose ($ out_file );??
Advanced Settings

We can also use the setPageWidth method to set the PDF width, setPageHeight to set the PDF height, and setHorizontalMargin to set the horizontal outer moment size; use the setVerticalMargin method to set the vertical outer moment (you can also use the setPageMargins method to set the outer margin. There are four parameters in total, corresponding to the upper right and lower left). You can use the setFooterHtml method to set the PDF footer, supported parameter values include % u (converted URL), % p (current page index value), and % n (total page quantity ).

More settings

I have noticed that apart from the above methods, there are setFooterUrl (set footer URL), setHeaderHtml (Set Top HTML), and setHeaderUrl (Set Top URL) setHeaderFooterPageExcludeList (whether to print the page header or footer), setPageNumberingOffset (index value on the first page of the PDF), enableImages (whether to support images), enableBackgrounds (whether to support background), setHtmlZoom) enableJavaScript (whether Javascript is displayed on PDF), enableHyperlinks (whether to display hyperlinks), setDefaultTextEncoding (default PDF text encoding, default: UTF-8), and usePrintMedia (whether to display the media type corresponding to CSS), setEncrypted (set whether to encrypt, no search engine after encryption) Method to Identify content), setUserPassword (set guest password), setOwnerPassword (set owner password), setNoPrint (whether printing is prohibited), setNoModify (whether modification is prohibited), setNoCopy (whether copying is prohibited) setPageLayout (page layout setting), setPageMode (page mode, whether to enable full screen), setinitial+zoomtype (applicable to screen setting when viewing PDF files), setinitial+exactzoom (initial zoom) setPdfScalingFactor, setPageBackgroundColor, setTransparentBackground, setAuthor, setWatermark, and setWatermarkRotation), setWatermarkInBac Kground, useSSL, numTokens, setMaxPages, and setFailOnNon200 (HTTP Error returned ).

Statement: This article uses BY-NC-SA protocol for authorization | IT passers-
Reprinted please note

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.