HTML2PDF - HTML to PDF Conversion - PHP Sample Code

Sample code for using Apryse SDK to directly convert HTML pages to PDF by using 'pdftron.PDF.HTML2PDF', provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, Go and VB. The HTML2PDF converter supports conversion from a string or URL and offers many options to control page size and formatting.

To use this code, you'll need to

  1. Download and get started with Server SDK
  2. Install the HTML2PDF Module

Learn more about our Server SDK and PDF Conversion Library.

1<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
4// Consult LICENSE.txt regarding license information.
5//---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
9
10// Relative path to the folder containing the test files.
11$input_path = getcwd()."/../../TestFiles/";
12$output_path = $input_path."Output/";
13
14//---------------------------------------------------------------------------------------
15// The following sample illustrates how to convert HTML pages to PDF format using
16// the HTML2PDF class.
17//
18// 'pdftron.PDF.HTML2PDF' is an optional PDFNet Add-On utility class that can be
19// used to convert HTML web pages into PDF documents by using an external module (html2pdf).
20//
21// html2pdf modules can be downloaded from https://dev.apryse.com/.
22//
23// Users can convert HTML pages to PDF using the following operations:
24// - Simple one line static method to convert a single web page to PDF.
25// - Convert HTML pages from URL or string, plus optional table of contents, in user defined order.
26// - Optionally configure settings for proxy, images, java script, and more for each HTML page.
27// - Optionally configure the PDF output, including page size, margins, orientation, and more.
28// - Optionally add table of contents, including setting the depth and appearance.
29//---------------------------------------------------------------------------------------
30
31 $output_path = "../../TestFiles/Output/html2pdf_example";
32 $host = "https://docs.apryse.com";
33 $page0 = "/";
34 $page1 = "/all-products/";
35 $page2 = "/web/faq";
36
37 // The first step in every application using PDFNet is to initialize the
38 // library and set the path to common PDF resources. The library is usually
39 // initialized only once, but calling Initialize() multiple times is also fine.
40 PDFNet::Initialize($LicenseKey);
41 PDFNet::GetSystemFontList(); // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.
42
43 // For HTML2PDF we need to locate the html2pdf module. If placed with the
44 // PDFNet library, or in the current working directory, it will be loaded
45 // automatically. Otherwise, it must be set manually using HTML2PDF.SetModulePath.
46 HTML2PDF::SetModulePath("./../../../PDFNetC/Lib");
47 if(!HTML2PDF::IsModuleAvailable()) {
48 echo "Unable to run HTML2PDFTest: PDFTron SDK HTML2PDF module not available.\n
49 ---------------------------------------------------------------\n
50 The HTML2PDF module is an optional add-on, available for download\n
51 at https://www.pdftron.com/. If you have already downloaded this\n
52 module, ensure that the SDK is able to find the required files\n
53 using the HTML2PDF::SetModulePath() function.\n";
54 return;
55 }
56
57 //--------------------------------------------------------------------------------
58 // Example 1) Simple conversion of a web page to a PDF doc.
59
60 $doc = new PDFDoc();
61
62 // now convert a web page, sending generated PDF pages to doc
63 $converter = new HTML2PDF();
64 $converter->InsertFromURL($host.$page0);
65 $converter->Convert($doc);
66 $doc->Save($output_path."_01.pdf", SDFDoc::e_linearized);
67 $doc->Close();
68
69 //--------------------------------------------------------------------------------
70 // Example 2) Modify the settings of the generated PDF pages and attach to an
71 // existing PDF document.
72
73 // open the existing PDF, and initialize the security handler
74 $doc = new PDFDoc("../../TestFiles/numbered.pdf");
75 $doc->InitSecurityHandler();
76
77 // create the HTML2PDF converter object and modify the output of the PDF pages
78 $converter = new HTML2PDF();
79 $converter->SetPaperSize(PrinterMode::e_11x17);
80
81 // insert the web page to convert
82 $converter->InsertFromURL($host.$page0);
83
84 // convert the web page, appending generated PDF pages to doc
85 $converter->Convert($doc);
86 $doc->Save($output_path."_02.pdf", SDFDoc::e_linearized);
87 $doc->Close();
88
89 //--------------------------------------------------------------------------------
90 // Example 3) Convert multiple web pages, adding a table of contents, and setting
91 // the first page as a cover page, not to be included with the table of contents outline.
92
93 $doc = new PDFDoc();
94
95 $converter = new HTML2PDF();
96 $header = "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:10px;color:#0000FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:10px;color:#0000FF'><span>PDFTRON HEADER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:10px;color:#0000FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>";
97 $footer = "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:7px;color:#FF00FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:7px;color:#FF00FF'><span>PDFTRON FOOTER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:7px;color:#FF00FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>";
98 $converter->SetHeader($header);
99 $converter->SetFooter($footer);
100 $converter->SetMargins("1cm", "2cm", ".5cm", "1.5cm");
101
102 $settings = new WebPageSettings();
103 $settings->SetZoom(0.5);
104 $converter->InsertFromURL($host.$page0, $settings);
105 $converter->Convert($doc);
106
107 //convert page 1 with the same settings, appending generated PDF pages to doc
108 $converter->InsertFromURL($host.$page1, $settings);
109 $converter->Convert($doc);
110
111 //convert page 2 with different settings, appending generated PDF pages to doc
112 $another_converter = new HTML2PDF();
113 $another_converter->SetLandscape(True);
114 $another_settings = new WebPageSettings();
115 $another_settings->SetPrintBackground(False);
116 $another_converter->InsertFromURL($host.$page2, $another_settings);
117 $another_converter->Convert($doc);
118
119 $doc->Save($output_path."_03.pdf", SDFDoc::e_linearized);
120 $doc->Close();
121
122 //--------------------------------------------------------------------------------
123 // Example 4) Convert HTML string to PDF.
124
125 $doc = new PDFDoc();
126
127 $converter = new HTML2PDF();
128
129 // Our HTML data
130 $html = "<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>";
131
132 // Add html data
133 $converter->InsertFromHtmlString($html);
134 // Note, InsertFromHtmlString can be mixed with the other Insert methods.
135
136 $converter->Convert($doc);
137 $doc->Save($output_path."_04.pdf", SDFDoc::e_linearized);
138 $doc->Close();
139
140 //--------------------------------------------------------------------------------
141 // Example 5) Set the location of the log file to be used during conversion.
142
143 $doc = new PDFDoc();
144
145 // now convert a web page, sending generated PDF pages to doc
146 $converter = new HTML2PDF();
147 $converter->SetLogFilePath("../../TestFiles/Output/html2pdf.log");
148 $converter->InsertFromURL($host.$page0);
149 $converter->Convert($doc);
150 $doc->Save($output_path."_05.pdf", SDFDoc::e_linearized);
151 $doc->Close();
152
153 PDFNet::Terminate();
154?>

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales