Adjust PDF Page Size with Aspose.HTML for Java

In today’s digital age, the need for generating high-quality PDFs from HTML content is on the rise. Aspose.HTML for Java is a powerful Java library that allows you to convert HTML documents to PDF format effortlessly. In this tutorial, we will focus on adjusting the page size when converting HTML to PDF using Aspose.HTML for Java.

Prerequisites

Before you begin, make sure you have the following prerequisites in place:

  • Java Development Environment: Ensure that you have Java Development Kit (JDK) installed on your system.
  • Aspose.HTML for Java: You need to download and install Aspose.HTML for Java from the website here.
  • HTML Document: You should have an HTML document ready for conversion. If not, create one or use an existing HTML file.

Import Packages

To start, you need to import the necessary packages to work with Aspose.HTML for Java. The following code snippet demonstrates how to do this:

import com.aspose.html.HTMLDocument;
import com.aspose.html.rendering.HtmlRenderer;
import com.aspose.html.rendering.pdf.PdfDevice;
import com.aspose.html.rendering.pdf.PdfRenderingOptions;
import com.aspose.html.drawing.Size;
import com.aspose.html.rendering.PageSetup;

Now that you have the prerequisites in place and have imported the necessary packages, let’s break down the process of adjusting the PDF page size into multiple steps:

Step 1: Reading HTML Content

First, you need to read the HTML content that you want to convert to PDF. In this example, we will read the HTML from a file named “FirstFile.html.”

try (java.io.FileInputStream fileInputStream = new java.io.FileInputStream(Resources.input("FirstFile.html"))) {

Step 2: Writing HTML Content

Next, you will write the HTML content to a file named “FirstFileOut.html.”

try (java.io.FileOutputStream fileOutputStream = new java.io.FileOutputStream(Resources.output("FirstFileOut.html"))) {
    byte[] bytes = new byte[fileInputStream.available()];
    fileInputStream.read(bytes);
    fileOutputStream.write(bytes);
    // Add custom HTML styles or content here
    String style = "<style>\n" +
                   ".st\n" +
                   "{\n" +
                   "color:\n" +
                   "green;\n" +
                   "}\n" +
                   "</style >\n" +
                   "<div id = id1 > Aspose.Html rendering Text in Black Color</div >\n" +
                   "<div id = id2 class='' st '' > Aspose.Html rendering Text in Green Color</div >\n" +
                   "<div id = id3 class='' st '' style = 'color: blue;' > Aspose.Html rendering Text in Blue Color</div >\n" +
                   "<div id = id3 class='' st '' style = 'color: red;' ><font face = 'Arial' > Aspose.Html rendering Text in Red\n" +
                   "Color</font ></div >\n";
    fileOutputStream.write(style.getBytes(java.nio.charset.StandardCharsets.UTF_8));
}

Step 3: Rendering HTML to PDF

Now, you will render the HTML content to a PDF file. We will cover two scenarios: one where the page size is not adjusted to the content width, and another where it is adjusted.

Page Size Not Adjusted

In this scenario, the page size is set to a fixed width and height, which may crop the content if it exceeds these dimensions.

String pdf_output;
com.aspose.html.rendering.HtmlRenderer pdf_renderer = new com.aspose.html.rendering.HtmlRenderer();

// Create an HTMLDocument instance from the HTML file
com.aspose.html.HTMLDocument html_document = new com.aspose.html.HTMLDocument(Resources.output("FirstFileOut.html"));

// Set PDF rendering options
com.aspose.html.rendering.pdf.PdfRenderingOptions pdf_options = new com.aspose.html.rendering.pdf.PdfRenderingOptions();
com.aspose.html.rendering.PageSetup pageSetup = new com.aspose.html.rendering.PageSetup();
pageSetup.setAnyPage(new com.aspose.html.drawing.Page(new com.aspose.html.drawing.Size(100, 100)));
pageSetup.setAdjustToWidestPage(false);
pdf_options.setPageSetup(pageSetup);

pdf_output = Resources.output("not-adjusted-to-widest-page_out.pdf");
com.aspose.html.rendering.pdf.PdfDevice device = new com.aspose.html.rendering.pdf.PdfDevice(pdf_options, pdf_output);

// Render the output
pdf_renderer.render(device, html_document);

Page Size Adjusted to Content Width

In this scenario, the page size is adjusted according to the content width.

com.aspose.html.rendering.pdf.PdfRenderingOptions pdf_options = new com.aspose.html.rendering.pdf.PdfRenderingOptions();
com.aspose.html.rendering.PageSetup pageSetup = new com.aspose.html.rendering.PageSetup();
pageSetup.setAnyPage(new com.aspose.html.drawing.Page(new com.aspose.html.drawing.Size(100, 100)));
pageSetup.setAdjustToWidestPage(true);
pdf_options.setPageSetup(pageSetup);

pdf_output = Resources.output("adjusted-to-widest-page_out.pdf");
device = new com.aspose.html.rendering.pdf.PdfDevice(pdf_options, pdf_output);

// Render the output
pdf_renderer.render(device, html_document);

Conclusion

In this tutorial, we have explored how to adjust the PDF page size when converting HTML to PDF using Aspose.HTML for Java. You have learned the prerequisites, imported the required packages, and followed a step-by-step guide to achieve this task. With Aspose.HTML for Java, you can easily control the page size of your generated PDFs, ensuring that your content is displayed as intended.

Feel free to experiment with different page sizes and settings to meet your specific requirements. If you encounter any issues or have further questions, don’t hesitate to seek assistance from the Aspose.HTML for Java documentation or the Aspose support forum.

FAQ’s

Q1: What is Aspose.HTML for Java?

A1: Aspose.HTML for Java is a Java library that allows you to work with HTML documents, manipulate, convert, and render them to various formats, including PDF.

Q2: How can I adjust the PDF page size when converting HTML to PDF with Aspose.HTML for Java?

A2: You can adjust the PDF page size by using the PageSetup class and setting the AdjustToWidestPage property to true or `false, depending on your requirements.

Q3: Can I customize the styling of HTML content before converting it to PDF?

A3: Yes, you can customize the styling by adding CSS and HTML elements to your HTML content before converting it to PDF, as demonstrated in the tutorial.

Q4: Where can I find the documentation for Aspose.HTML for Java?

A4: You can refer to the documentation here for comprehensive information and examples.

Q5: Is there a free trial available for Aspose.HTML for Java?

A5: Yes, you can access a free trial of Aspose.HTML for Java from this link.