Convert HTML to PDF in Python
Python HTML to PDF Conversion
Aspose.PDF for Python is a PDF manipulation API that lets you convert any existing HTML documents to PDF seamlessly. The process of converting HTML to PDF can be flexibly customized.
Convert HTML to PDF
The following Python code sample shows how to convert an HTML document to a PDF.
- Create an instance of the HtmlLoadOptions class.
- Initialize Document object.
- Save output PDF document by calling document.save() method.
from os import path
import aspose.pdf as ap
import requests
import io
path_infile = path.join(self.data_dir, infile)
path_outfile = path.join(self.data_dir, "python", outfile)
load_options = ap.HtmlLoadOptions()
load_options.page_layout_option = ap.HtmlPageLayoutOption.SCALE_TO_PAGE_WIDTH
document = ap.Document(path_infile, load_options)
document.save(path_outfile)
print(infile + " converted into " + outfile)
Try to convert HTML to PDF online
Aspose presents you online free application “HTML to PDF”, where you may try to investigate the functionality and quality it works.
Convert HTML to PDF using media type
This example shows how to convert an HTML file to PDF using Aspose.PDF for Python via .NET with specific rendering options.
- Create an instance of the HtmlLoadOptions() class. The ‘html_media_type’ applies CSS rules intended for on-screen display. The ‘html_media_type’ property can have multiple values. You can set it to HtmlMediaType.SCREEN or HtmlMediaType.PRINT.
- Load the HTML into an ap.Document using the load options.
- Save the document as a PDF.
from os import path
import aspose.pdf as ap
import requests
import io
path_infile = path.join(self.data_dir, infile)
path_outfile = path.join(self.data_dir, "python", outfile)
load_options = ap.HtmlLoadOptions()
load_options.html_media_type = ap.HtmlMediaType.SCREEN
document = ap.Document(path_infile, load_options)
document.save(path_outfile)
print(infile + " converted into " + outfile)
Convert HTML to PDF priority CSS Page Rule
Some documents may contain layout settings that utilize the Page rule, which can create ambiguity when generating the layout. You can control the priority using the ‘is_priority_css_page_rule’ property. If this property is set to ‘True’, the CSS rule is applied first.
- Create an instance of the HtmlLoadOptions class.
- Set ‘is_priority_css_page_rule = False’ to disable prioritizing @page CSS rules, allowing other styles to take precedence.
- Load the HTML into an ap.Document with the configured options.
- Save the document as a PDF.
from os import path
import aspose.pdf as ap
import requests
import io
path_infile = path.join(self.data_dir, infile)
path_outfile = path.join(self.data_dir, "python", outfile)
load_options = ap.HtmlLoadOptions()
# load_options.is_priority_css_page_rule = False
document = ap.Document(path_infile, load_options)
document.save(path_outfile)
print(infile + " converted into " + outfile)
Convert HTML to PDF with Embeded Fonts
This example shows how to convert an HTML file to PDF while embedding fonts. If you need a PDF document with Embedded Fonts, you should set ‘is_embed_fonts’ to True.
- Create ‘HtmlLoadOptions()’ to configure HTML to PDF conversion.
- Set ‘is_embed_fonts = True’ to ensure that all fonts used in the HTML are embedded directly into the PDF, preserving visual fidelity.
- Load the HTML into an ap.Document with these options.
- Save the document as a PDF.
from os import path
import aspose.pdf as ap
import requests
import io
path_infile = path.join(self.data_dir, infile)
path_outfile = path.join(self.data_dir, "python", outfile)
load_options = ap.HtmlLoadOptions()
load_options.is_embed_fonts = True
document = ap.Document(path_infile, load_options)
document.save(path_outfile)
print(infile + " converted into " + outfile)
Render Content on single Page during HTML to PDF conversion
This example demonstrates how to convert an HTML file into a single-page PDF using Aspose.PDF for Python. You can display all content on one page using the ‘is_render_to_single_page property’.
- Create an instance of ‘HtmlLoadOptions()’ to configure the conversion process.
- Enable ‘is_render_to_single_page’ to render the entire HTML content onto a single continuous PDF page.
- Load the document with the configured options into an ‘ap.Document’.
- Save the result as a PDF file.
from os import path
import aspose.pdf as ap
import requests
import io
path_infile = path.join(self.data_dir, infile)
path_outfile = path.join(self.data_dir, "python", outfile)
options = ap.HtmlLoadOptions()
options.is_render_to_single_page = True
doc = ap.Document(path_infile, options)
doc.save(path_outfile)
Convert MHTML to PDF
This example shows how to convert an MHT (MHTML) file into a PDF document using Aspose.PDF for Python with specific page dimensions.
- Create an instance of ap.MhtLoadOptions() to configure MHT file processing.
- Set various parameters, such as page size.
- Initialize the document with the input file and configured loading options.
- Save the resulting document as a PDF.
from os import path
import aspose.pdf as ap
import requests
import io
path_infile = path.join(self.data_dir, infile)
path_outfile = path.join(self.data_dir, "python", outfile)
load_options = ap.MhtLoadOptions()
load_options.page_info.width = 842
load_options.page_info.height= 1191
document = ap.Document(path_infile, load_options)
document.save(path_outfile)
print(infile + " converted into " + outfile)