Skip to main content
Skip table of contents

Scaling large HTML content to render onto smaller PDF page sizes

There are often instances where you need to convert HTML content that is larger then the default page size of your PDF. This will cause the generated PDF file to have content that overlaps, or with content rendering off the page. 

The simplest solution would be to change the PDF page size to one that actually fits your HTML content. However, you most probably want to generate a PDF of a specific page size (such as A4 or Letter).

You might also try fiddling with fonts or margin sizes, but this isn't always possible of course. Let's consider a situation where your company specifies not just a certain page size, but their branding guidelines require using certain fonts, page margins etc. What do you do then?

A much better solution is to rescale the HTML content to the required page size. To do this, we can take the following steps:

  1. Convert your HTML to a default page size that is at least big enough to contain the HTML content
  2. Iterate through the pages, and copy each page as a formXObject
  3. For each page formXObject, scale it with 0.4 coefficient (or whatever you deem appropriate)
  4. Add the content to the resultant document with the page size you wish to have.

An example demonstrating this process is below:

JAVA

JAVA

import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class Resize {
    static String inputHtml ="inputHtml.html";
    static String outputPdf = "outputPdf.pdf";
    public static void main(String[] args) throws IOException {
        ByteArrayOutputStream ms = new ByteArrayOutputStream();
        PdfDocument pdfDocument = new PdfDocument(new PdfWriter(ms));
        pdfDocument.setDefaultPageSize(PageSize.A3);
        HtmlConverter.convertToPdf(new FileInputStream(inputHtml),pdfDocument);
        PdfDocument resultantDocument = new PdfDocument(new PdfWriter(outputPdf));
        resultantDocument.setDefaultPageSize(PageSize.A4);
        pdfDocument = new PdfDocument(new PdfReader(new ByteArrayInputStream(ms.toByteArray())));
        for (int i = 1; i <= pdfDocument.getNumberOfPages(); i++) {
            PdfPage page = pdfDocument.getPage(i);
            PdfFormXObject formXObject = page.copyAsFormXObject(resultantDocument);
            PdfCanvas pdfCanvas = new PdfCanvas(resultantDocument.addNewPage());
            pdfCanvas.addXObject(formXObject, 0.4f, 0, 0, 0.4f, 6, 350);
        }
        pdfDocument.close();
        resultantDocument.close();
    }
}

C#

C#
using iText.Html2pdf;

using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Xobject;
using System;
using System.IO;

namespace ConsoleApp15
{
    class Program
    {
        static String outputPdf = "outputPdfFile.pdf";
        static String inputHtml = "inputHtml.html";
        static void Main(string[] args)
        {
            MemoryStream ms = new MemoryStream();
            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(ms));
            pdfDocument.SetDefaultPageSize(PageSize.A3);
            using (FileStream htmlSource = File.Open(inputHtml, FileMode.Open))
            using (FileStream pdfDest = File.Open(outputPdf, FileMode.OpenOrCreate))
            {
                ConverterProperties converterProperties = new ConverterProperties();
                HtmlConverter.ConvertToPdf(htmlSource, pdfDocument,converterProperties);
            }
            PdfDocument resultantDocument = new PdfDocument(new PdfWriter(outputPdf));
            resultantDocument.SetDefaultPageSize(PageSize.A4);
            MemoryStream k = new MemoryStream(ms.ToArray());
            pdfDocument = new PdfDocument(new PdfReader(k));
            for (int i = 1; i <= pdfDocument.GetNumberOfPages(); i++)
            {
                PdfPage page = pdfDocument.GetPage(i);
                PdfFormXObject formXObject = page.CopyAsFormXObject(resultantDocument);
                PdfCanvas pdfCanvas = new PdfCanvas(resultantDocument.AddNewPage());
                // 3a and 3b
                pdfCanvas.AddXObject(formXObject, 0.4f, 0, 0, 0.4f, 6, 350);
            }
            pdfDocument.Close();
            resultantDocument.Close();
        }
    }
}



JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.