pdfHTML: Responsive Design

This is a code example of iText PDF, discover more.

7th June 2017
iText PDF

Switch code for this example

pdfHTMLResponsiveDesign.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. package com.itextpdf.samples.pdfHTML.ResponsiveDesign;
  10.  
  11. import com.itextpdf.html2pdf.ConverterProperties;
  12. import com.itextpdf.html2pdf.HtmlConverter;
  13. import com.itextpdf.kernel.geom.PageSize;
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfWriter;
  16. import com.itextpdf.layout.font.FontProvider;
  17. import com.itextpdf.licensekey.LicenseKey;
  18. import com.itextpdf.styledxmlparser.css.media.MediaDeviceDescription;
  19. import com.itextpdf.styledxmlparser.css.media.MediaType;
  20. import com.itextpdf.styledxmlparser.css.util.CssUtils;
  21.  
  22. import java.io.File;
  23. import java.io.FileInputStream;
  24. import java.io.IOException;
  25.  
  26. public class pdfHTMLResponsiveDesign {
  27.  
  28.     public static final String sourceFolder = "src/test/resources/pdfHTML/ResponsiveDesign/";
  29.     public static final String destinationFolder = "target/output/pdfHTML/ResponsiveDesign/";
  30.     public static final String[] files = {"responsive"};
  31.  
  32.     public static final PageSize[] pageSizes = {
  33.             PageSize.A4.rotate(),
  34.             new PageSize(720, PageSize.A4.getHeight()),
  35.             new PageSize(PageSize.A5.getWidth(), PageSize.A4.getHeight())
  36.     };
  37.  
  38.     //Path to the license file
  39.     public static final String LICENSE = "src/test/resources/pdfHTML/itextkey-html2pdf_typography.xml";
  40.  
  41.     public static void main(String[] args) throws IOException, InterruptedException {
  42.         LicenseKey.loadLicenseFile(LICENSE);
  43.         for (String name : files) {
  44.             String htmlSource = sourceFolder + name + "/" + name + ".html";
  45.             String resourceFolder = sourceFolder + name + "/";
  46.             String pdfDest = destinationFolder + name + "/" + name + ".pdf";
  47.             File file = new File(pdfDest);
  48.  
  49.             System.out.println("Parsing: " + htmlSource);
  50.             file.getParentFile().mkdirs();
  51.             pdfHTMLResponsiveDesign runner = new pdfHTMLResponsiveDesign();
  52.             //Create a pdf for each page size
  53.             for (int i = 0; i < pageSizes.length; i++) {
  54.                 float width = CssUtils.parseAbsoluteLength("" + pageSizes[i].getWidth());
  55.                 String dest = destinationFolder + name + "/" + width + "/" + name + "_" + width + ".pdf";
  56.                 runner.parseMedia(htmlSource, dest, resourceFolder, pageSizes[i], width);
  57.             }
  58.  
  59.         }
  60.     }
  61.  
  62.     public void parseMedia(String htmlSource, String pdfDest, String resourceLoc, PageSize pageSize, float screenWidth) throws IOException {
  63.         File pdf = new File(pdfDest);
  64.         pdf.getParentFile().mkdirs();
  65.  
  66.         PdfWriter writer = new PdfWriter(pdfDest);
  67.         PdfDocument pdfDoc = new PdfDocument(writer);
  68.  
  69.         //Set the result to be tagged
  70.         pdfDoc.setTagged();
  71.         pdfDoc.setDefaultPageSize(pageSize);
  72.  
  73.         ConverterProperties converterProperties = new ConverterProperties();
  74.  
  75.         //Set media device description details
  76.         MediaDeviceDescription mediaDescription = new MediaDeviceDescription(MediaType.SCREEN);
  77.         mediaDescription.setWidth(screenWidth);
  78.         converterProperties.setMediaDeviceDescription(mediaDescription);
  79.  
  80.         FontProvider fp = new FontProvider();
  81.         fp.addStandardPdfFonts();
  82.         //Register external font directory
  83.         fp.addDirectory(resourceLoc);
  84.  
  85.         converterProperties.setFontProvider(fp);
  86.         converterProperties.setBaseUri(resourceLoc);
  87.  
  88.         //Create acroforms from text and button input fields
  89.         converterProperties.setCreateAcroForm(true);
  90.  
  91.         try (FileInputStream fileInputStream = new FileInputStream(htmlSource)) {
  92.             HtmlConverter.convertToPdf(fileInputStream, pdfDoc, converterProperties);
  93.         }
  94.         pdfDoc.close();
  95.     }
  96. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now