# 1. Install IronPDF: pip install ironpdf# 2. Import the libraryfrom ironpdf import *# 3. Create rendererrenderer = ChromePdfRenderer()# 4. Convert HTML to PDFpdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")# 5. Save the PDFpdf.SaveAs("output.pdf")
# 1. Install IronPDF: pip install ironpdf
# 2. Import the library
from ironpdf import *
# 3. Create renderer
renderer = ChromePdfRenderer()
# 4. Convert HTML to PDF
pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")
# 5. Save the PDF
pdf.SaveAs("output.pdf")
from ironpdf import *# Apply license keyLicense.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"# Instantiate the Chromium rendererrenderer = ChromePdfRenderer()# Convert an HTML string to a PDF documentpdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1><p>This is an example HTML string.</p>")
from ironpdf import *
# Apply license key
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
# Instantiate the Chromium renderer
renderer = ChromePdfRenderer()
# Convert an HTML string to a PDF document
pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1><p>This is an example HTML string.</p>")
Python
如何保存生成的 PDF?
一旦HTML字符串已转换为SaveAs:
# Save the PDF to a filepdf.SaveAs("htmlstring_to_pdf.pdf")
# Save the PDF to a file
pdf.SaveAs("htmlstring_to_pdf.pdf")
from ironpdf import *# Apply license keyLicense.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"# Instantiate the rendererrenderer = ChromePdfRenderer()# Create a PDF from a local HTML filepdf = renderer.RenderHtmlFileAsPdf("example.html")# Save the output PDFpdf.SaveAs("htmlfile_to_pdf.pdf")
from ironpdf import *
# Apply license key
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
# Instantiate the renderer
renderer = ChromePdfRenderer()
# Create a PDF from a local HTML file
pdf = renderer.RenderHtmlFileAsPdf("example.html")
# Save the output PDF
pdf.SaveAs("htmlfile_to_pdf.pdf")
from ironpdf import *# Apply license keyLicense.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"# Instantiate the rendererrenderer = ChromePdfRenderer()# Convert a web page to PDFpdf = renderer.RenderUrlAsPdf("https://ironpdf.com")# Save the output PDFpdf.SaveAs("url.pdf")
from ironpdf import *
# Apply license key
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
# Instantiate the renderer
renderer = ChromePdfRenderer()
# Convert a web page to PDF
pdf = renderer.RenderUrlAsPdf("https://ironpdf.com")
# Save the output PDF
pdf.SaveAs("url.pdf")
from ironpdf import *License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"renderer = ChromePdfRenderer()pdf = renderer.RenderUrlAsPdf("https://ironpdf.com")# Set user password (required to open the PDF)pdf.SecuritySettings.UserPassword = "sharable"# Set owner password (controls permissions)pdf.SecuritySettings.OwnerPassword = "admin123"# Configure document permissionspdf.SecuritySettings.AllowUserPrinting = Truepdf.SecuritySettings.AllowUserCopyPasteContent = False# Save the password-protected PDFpdf.SaveAs("protected.pdf")
from ironpdf import *
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
renderer = ChromePdfRenderer()
pdf = renderer.RenderUrlAsPdf("https://ironpdf.com")
# Set user password (required to open the PDF)
pdf.SecuritySettings.UserPassword = "sharable"
# Set owner password (controls permissions)
pdf.SecuritySettings.OwnerPassword = "admin123"
# Configure document permissions
pdf.SecuritySettings.AllowUserPrinting = True
pdf.SecuritySettings.AllowUserCopyPasteContent = False
# Save the password-protected PDF
pdf.SaveAs("protected.pdf")
from ironpdf import *# Apply your license keyLicense.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"# --- HTML string to PDF ---renderer = ChromePdfRenderer()pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1><p>This is an example HTML string.</p>")pdf.SaveAs("htmlstring_to_pdf.pdf")# --- Local HTML file to PDF ---renderer = ChromePdfRenderer()pdf = renderer.RenderHtmlFileAsPdf("example.html")pdf.SaveAs("htmlfile_to_pdf.pdf")# --- URL to PDF ---renderer = ChromePdfRenderer()pdf = renderer.RenderUrlAsPdf("https://ironpdf.com")pdf.SaveAs("url.pdf")# --- Password-protected PDF ---pdf.SecuritySettings.UserPassword = "sharable"pdf.SecuritySettings.OwnerPassword = "admin123"pdf.SecuritySettings.AllowUserPrinting = Truepdf.SecuritySettings.AllowUserCopyPasteContent = Falsepdf.SaveAs("protected.pdf")
from ironpdf import *
# Apply your license key
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
# --- HTML string to PDF ---
renderer = ChromePdfRenderer()
pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1><p>This is an example HTML string.</p>")
pdf.SaveAs("htmlstring_to_pdf.pdf")
# --- Local HTML file to PDF ---
renderer = ChromePdfRenderer()
pdf = renderer.RenderHtmlFileAsPdf("example.html")
pdf.SaveAs("htmlfile_to_pdf.pdf")
# --- URL to PDF ---
renderer = ChromePdfRenderer()
pdf = renderer.RenderUrlAsPdf("https://ironpdf.com")
pdf.SaveAs("url.pdf")
# --- Password-protected PDF ---
pdf.SecuritySettings.UserPassword = "sharable"
pdf.SecuritySettings.OwnerPassword = "admin123"
pdf.SecuritySettings.AllowUserPrinting = True
pdf.SecuritySettings.AllowUserCopyPasteContent = False
pdf.SaveAs("protected.pdf")
What are the main methods for creating PDFs in IronPDF?
The main methods for creating PDFs in IronPDF include converting an HTML string, a local HTML file, a live URL, and securing the PDF with passwords. Each method is designed to handle different types of content transformations.
How can I ensure that my generated PDFs are not watermarked when using IronPDF?
You need to configure a valid license key before generating PDFs to ensure they are not watermarked. Assign the license key to the `LicenseKey` attribute in your script to remove the trial watermark.