Some test text!

menu

Javascript HTML to PDF converter

Sample Javascript code for using PDFTron SDK to directly convert HTML pages to PDF by using 'pdftron.PDF.HTML2PDF'. The HTML2PDF converter supports conversion from a string or URL and offers many options to control page size and formatting. Learn more about our Javascript PDF Library and PDF Conversion Library.

Get StartedSamplesDownload

To run this sample, get started with a free trial of PDFTron SDK.

//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------
// The following sample illustrates how to convert HTML pages to PDF format using
// the HTML2PDF class.
// 
// 'HTML2PDF' is an optional PDFNet Add-On utility class that can be 
// used to convert HTML web pages into PDF documents by using an external module (html2pdf).
//
// Users can convert HTML pages to PDF using the following operations:
// - Simple one line static method to convert a single web page to PDF. 
// - Convert HTML pages from URL or string, plus optional table of contents, in user defined order. 
// - Optionally configure settings for proxy, images, java script, and more for each HTML page. 
// - Optionally configure the PDF output, including page size, margins, orientation, and more. 
// - Optionally add table of contents, including setting the depth and appearance.
//---------------------------------------------------------------------------------------

const { PDFNet } = require('../../lib/pdfnet.js');

((exports) => {
  'use strict';

  exports.runHTML2PDFTest = () => {
    const main = async () => {
      const output_path = '../TestFiles/Output/';
      const host = 'http://www.gutenberg.org/';
      const page0 = 'wiki/Main_Page';
      const page1 = 'catalog/';
      const page2 = 'browse/recent/last1';
      const page3 = 'wiki/Gutenberg:The_Sheet_Music_Project';

      await PDFNet.HTML2PDF.setModulePath('../../lib');

      //--------------------------------------------------------------------------------
      // Example 1) Simple conversion of a web page to a PDF doc. 

      try {
        const html2pdf = await PDFNet.HTML2PDF.create();
        const doc = await PDFNet.PDFDoc.create();

        html2pdf.insertFromUrl(host.concat(page0));
        // now convert a web page, sending generated PDF pages to doc
        await html2pdf.convert(doc);
        doc.save(output_path.concat('_01.pdf'), PDFNet.SDFDoc.SaveOptions.e_linearized);
      } catch (err) {
        console.log(err);
      }

      //--------------------------------------------------------------------------------
      // Example 2) Modify the settings of the generated PDF pages and attach to an
      // existing PDF document. 

      try {
        // open the existing PDF, and initialize the security handler
        const doc = await PDFNet.PDFDoc.createFromFilePath('../TestFiles/numbered.pdf');
        await doc.initSecurityHandler();

        // create the HTML2PDF converter object and modify the output of the PDF pages
        const html2pdf = await PDFNet.HTML2PDF.create();
        html2pdf.setImageQuality(25);
        html2pdf.setPaperSize2('11in', '17in');

        // insert the web page to convert
        html2pdf.insertFromUrl(host.concat(page0));
        // convert the web page, appending generated PDF pages to doc
        await html2pdf.convert(doc);
        doc.save(output_path.concat('_02.pdf'), PDFNet.SDFDoc.SaveOptions.e_linearized);
      } catch (err) {
        console.log(err);
      }

      //--------------------------------------------------------------------------------
      // Example 3) Convert multiple web pages, adding a table of contents, and setting
      // the first page as a cover page, not to be included with the table of contents outline. 

      try {
        // Add a cover page, which is excluded from the outline, and ignore any errors
        const cover = await PDFNet.HTML2PDF.WebPageSettings.create();
        cover.setLoadErrorHandling(PDFNet.HTML2PDF.WebPageSettings.ErrorHandling.e_ignore);
        await cover.setIncludeInOutline(false);

        const html2pdf = await PDFNet.HTML2PDF.create();
        html2pdf.insertFromUrl2(host.concat(page3), cover);

        // Add a table of contents settings (modifying the settings is optional)
        const toc = await PDFNet.HTML2PDF.TOCSettings.create();
        await toc.setDottedLines(false);
        html2pdf.insertTOC2(toc);

        // Now add the rest of the web pages, disabling external links and 
        // skipping any web pages that fail to load.
        //
        // Note that the order of insertion matters, so these will appear
        // after the cover and table of contents, in the order below.
        const settings = await PDFNet.HTML2PDF.WebPageSettings.create();
        settings.setLoadErrorHandling(PDFNet.HTML2PDF.WebPageSettings.ErrorHandling.e_skip);
        await settings.setExternalLinks(false);

        html2pdf.insertFromUrl2(host.concat(page0), settings);
        html2pdf.insertFromUrl2(host.concat(page1), settings);
        html2pdf.insertFromUrl2(host.concat(page2), settings);

        const doc = await PDFNet.PDFDoc.create();
        await html2pdf.convert(doc);
        doc.save(output_path.concat('_03.pdf'), PDFNet.SDFDoc.SaveOptions.e_linearized);
      } catch (err) {
        console.log(err);
      }

      //--------------------------------------------------------------------------------
      // Example 4) Convert HTML string to PDF. 

      try {
        const html2pdf = await PDFNet.HTML2PDF.create();
        const doc = await PDFNet.PDFDoc.create();
        const html = '<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>'

        html2pdf.insertFromHtmlString(html);
        await html2pdf.convert(doc);
        doc.save(output_path.concat('_04.pdf'), PDFNet.SDFDoc.SaveOptions.e_linearized);
      } catch (err) {
        console.log(err);
      }

      console.log('Test Complete!')
    }
    PDFNet.runWithCleanup(main, 0).then(function () { PDFNet.shutdown(); }); // replace with your own license key and remove the license-key.js script tag
  };
  exports.runHTML2PDFTest();
})(exports);
// eslint-disable-next-line spaced-comment
//# sourceURL=HTML2PDFTest.js
close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

Join our live demo to learn about use cases & capabilities for WebViewer

Learn more
close