HTML文件到PDF
Convert an entire HTML file into a pixel-perfect PDF with IronPDF for Python’s RenderHtmlFileAsPdf method.
开始
To convert an HTML file to PDF, you can use the RenderHtmlFileAsPdf method. This method requires only the path to the local HTML file and returns a PdfDocument object. 此对象包含来自 HTML 文件的内容,呈现效果如同在网络浏览器中查看。 确保您的 HTML 包含有效标记以避免渲染问题。
下面是一个如何使用该方法的简单示例:
理解代码
ChromePdfRenderer:该对象用作与渲染过程的交互点。 它允许通过设置头、脚和更多属性来调整 PDF 输出。
ChromePdfRenderer: This method takes a path to an HTML file and converts it into aPdfDocument. 生成的 PDF 将与 HTML 文件在网络浏览器中的外观非常匹配。save_as: This method saves the newly created PDF document to the specified path on your file system.
自定义 PDF 输出
ChromePdfRenderer comes with various options for customization, including setting headers, footers, margins, backgrounds, page numbers, etc. For more advanced features and customization, see this code example and learn how to customize PDF output.






