Last active
February 16, 2022 22:39
How to Convert HTML file to PDF in Java. For more information, please follow link: https://kb.aspose.com/html/java/how-to-convert-html-file-to-pdf-in-java/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.aspose.html.License; | |
import com.aspose.html.drawing.Color; | |
import com.aspose.html.drawing.Size; | |
import com.aspose.html.drawing.Page; | |
import com.aspose.html.drawing.Length; | |
import com.aspose.html.rendering.PageSetup; | |
import com.aspose.html.saving.PdfSaveOptions; | |
public class ConvertHtmlFileToPdfInJava { | |
public static void main(String[] args) throws Exception { //main function to convert HTML to PDF in Java | |
// Load Aspose.Html license to avoid watermark in the output PDF file | |
License licenseForHtmlToPdf = new License(); | |
licenseForHtmlToPdf.setLicense("Aspose.Html.lic"); | |
// Initialize PdfSaveOptions class object to customize PDF generated from HTML | |
PdfSaveOptions pdfSaveOptionsObj = new PdfSaveOptions(); | |
// Initialize PageSetup and Page classes object | |
PageSetup pageSetupObj = new PageSetup(); | |
Page pageObj = new Page(); | |
// Set page size to A4 i.e. 8.25 x 11.75 inches | |
pageObj.setSize(new Size(Length.fromInches(8.25f),Length.fromInches(11.75f))); | |
// Set the page for the page setup object | |
pageSetupObj.setAnyPage(pageObj); | |
// Set the page setup for the PdfSaveOptions class object | |
pdfSaveOptionsObj.setPageSetup(pageSetupObj); | |
// Now, applying Tan color to background | |
pdfSaveOptionsObj.setBackgroundColor(Color.getTan()); | |
// Convert HTML document to PDF | |
com.aspose.html.converters.Converter.convertHTML("FirstFile.html",pdfSaveOptionsObj, | |
"outputPdfForGeneratedHtml.pdf"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment