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:
- Convert your HTML to a default page size that is at least big enough to contain the HTML content
- Iterate through the pages, and copy each page as a
formXObject
- For each page
formXObject
, scale it with 0.4 coefficient (or whatever you deem appropriate) - Add the content to the resultant document with the page size you wish to have.
An example demonstrating this process is below: