Articles in this section
Category / Section

How to resize the HTML content in PDF during HTML to PDF conversion?

1 min read

The HTML-to-PDF converter is a .NET PDF Library for converting webpages, SVG, MHTML, and HTML files to PDF using C#. It uses the popular rendering engine Blink (Google Chrome). It is reliable and accurate. The result preserves all graphics, images, text, fonts, and the layout of the original HTML document or webpage.

Using this library, we can adjust the HTML content size in PDF documents using C# and VB.NET.

Default Viewport size: By default, HTML to PDF converter using 1920 x 0 as ViewPortSize for the conversion.

Customize Viewport size: The ViewPortSize will be used to set the Blink browser window size. If the viewport width is smaller than the HTML content width, the Blink HTML converter will adjust the viewport width to match the HTML content width. Additionally, the Blink HTML converter dynamically calculates the height of the HTML based on the viewport width, ensuring accurate rendering of the content within the specified viewport.

Steps to resize the HTML content size in PDF document programmatically:

  1. Create a new C# console application project.

    Console application creation

  2. Install the Syncfusion.HtmlToPdfConverter.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.

    NuGet_package installation

  3. Include the following namespaces in the Program.cs file.

C#

using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;

VB.NET

Imports Syncfusion.HtmlConverter
Imports Syncfusion.Pdf
  1. Use the following code example to resize the HTML content size in a PDF document using ViewPortSize property of Blink HTML converter.
    C#
//Initialize the HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

//Initialize blink converter settings. 
BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
//Set Blink viewport size.
blinkConverterSettings.ViewPortSize = new Size(400, 0);
//Assign Blink converter settings to HTML converter.
htmlConverter.ConverterSettings = blinkConverterSettings;

//Convert URL to PDF document. 
PdfDocument document = htmlConverter.Convert(Path.GetFullPath("Input.html"));        

//Save and close the PDF document.
document.Save("HTML-to-PDF.pdf");
document.Close(true);
//This will open the PDF file so, the result will be seen in default PDF Viewer. 
Process.Start("HTML-to-PDF.pdf");

VB.NET

'Initialize the HTML to PDF converter.
Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()
'Initialize blink converter settings. 
Dim blinkConverterSettings As BlinkConverterSettings = New BlinkConverterSettings()
'Set Blink viewport size.
blinkConverterSettings.ViewPortSize = New Size(400, 0)
'Assign Blink converter settings to HTML converter.
htmlConverter.ConverterSettings = blinkConverterSettings
'Convert URL to PDF document. 
Dim document As PdfDocument = htmlConverter.Convert(Path.GetFullPath("Input.html"))
'Save and close the PDF document.
document.Save("HTML-to-PDF.pdf")
document.Close(True)
'This will open the PDF file so, the result will be seen in default PDF Viewer. 
Process.Start("HTML-to-PDF.pdf")

You can download the work sample from HTML-to-PDF-sample.zip.

Please refer below screen shots of result with different viewport size:

Viewport Screenshot
Default system viewport size
Default viewport size
Custom viewport size (400 width)
Custom viewport size

Take a moment to peruse the documentation, where you can find other options like converting HTML to PDF, URL to PDF, HTML string to PDF, etc.

Refer here to explore the HTML to PDF features in Syncfusion Essential PDF.

An online sample link for converting HTML to PDF document.

Note: Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

Conclusion
I hope you enjoyed learning about how to resize the HTML content in PDF during HTML to PDF conversion.

You can refer to our WinForms PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day
free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied