How to Create A PDF File in Python
Tags: python, webdev, softwaredevelopment, ironpdf
Published: 2024-06-24 05:20:14

###How to Create a PDF File in Python

1. Begin by creating a new Python project or opening an existing one.
2. Install the IronPDF library using pip to enable PDF manipulation capabilities.
3. Initialize a ChromePdfRenderer() object within your Python script, which serves as the engine to convert HTML content into PDF format.
4. Utilize IronPDF's functionality to render HTML content into a PDF document. This step involves specifying the HTML content or file path that you want to convert.
5. Save the generated PDF file to a specified location on your system, ensuring the document is stored and accessible for future use or distribution. 

Creating [PDF](https://en.wikipedia.org/wiki/PDF) files (Portable document format) programmatically can be useful for generating reports, invoices, or any other documents in a structured format. [IronPDF](https://ironpdf.com/) is a powerful library that allows you to create, manipulate, and read PDF documents in Python. This tutorial will guide you through the steps to install IronPDF, create a simple PDF, and explore some advanced features.

Introduction to IronPDF

IronPDF is a powerful and flexible Python library for a variety of PDF tasks, including [creating](https://ironpdf.com/blog/using-ironpdf/csharp-html-to-pdf-example/), [editing](https://ironpdf.com/tutorials/csharp-edit-pdf-complete-tutorial/), and [extracting](https://ironpdf.com/blog/using-ironpdf/csharp-extract-text-from-pdf/) content. It offers features like converting [HTML to PDF](https://ironpdf.com/tutorials/html-to-pdf/), [merging PDFs](https://ironpdf.com/examples/merge-pdfs/), adding [watermarks](https://ironpdf.com/examples/pdf-watermarking/), and accurately [extracting](https://ironpdf.com/blog/using-ironpdf/csharp-extract-text-from-pdf/) text and images. A key strength is its ability to preserve the integrity and formatting of complex PDFs during edits. This makes it ideal for generating reports, archiving web pages, and automating document workflows, helping to save time and boost productivity. With its extensive API and easy integration into Python projects, IronPDF is an essential tool for developers needing efficient and effective PDF management.

##Step By Step Tutorial:

Let's begin a step-by-step tutorial to generate PDF documents.

###Step # 1: Create or Open Python Project:

The first step is to create a new Python project or open an existing one in your preferred IDE. I am using Microsoft Visual Studio, but you can use any IDE you prefer. The steps will remain the same.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8w4zhi5zos9vx21f3slt.png)
###Step # 2: Install IronPDF:

Next, install the IronPDF library using pip. Keep in mind that IronPDF requires a .NET runtime to be installed on your machine. Run the following command:

```
pip install IronPDF
```

###Step # 3: Create PDF files

IronPDF offers three methods for creating PDF files:

1. Create a PDF document from an HTML string
2. Create a  PDF file from a URL
3. Generate PDF Files from an HTML file

Let's explore each method one by one.

###Create a PDF Document from an HTLM String:

Creating a PDF file from an HTML string allows you to dynamically generate documents from formatted text and elements directly within your Python application. This approach is particularly useful for generating reports, invoices, and other documents where content structure is important. Below, we'll demonstrate how to use IronPDF to render HTML content into a PDF file seamlessly.

```
from ironpdf import *

# Apply your license key
License.LicenseKey = "IRONSUITE.ABC.TRIAL-G43TTA.TRIAL.EXPIRES.20.MAY.2025"

renderer = ChromePdfRenderer()

pdfFromHtmlString = renderer.RenderHtmlAsPdf("<h1>Hello World</h1><p>This is a sample PDF created with IronPDF.</p>")
pdfFromHtmlString.SaveAs("sample_pdf.pdf")
```

This code snippet demonstrates how to use IronPDF to convert an HTML string into a PDF file.

1. The first line applies the license key required to use IronPDF, ensuring that the library functions correctly within your application. You may get a free trial license from the [IronPDF Official Website](https://ironpdf.com/#trial-license). 
2. The ChromePdfRenderer() initializes an instance of the renderer, which is capable of converting HTML content into a PDF format. This renderer leverages the Chromium browser engine under the hood to accurately render complex HTML structures.
3. renderer.RenderHtmlAsPdf() method takes an HTML string as input and converts it into a PDF document (pdfFromHtmlString).
4. Finally, pdfFromHtmlString.SaveAs("sample_pdf.pdf") saves the generated PDF file with the specified filename ("sample_pdf.pdf") in the current directory.

This approach is useful for dynamically generating PDF documents from HTML content, allowing for flexible document creation and formatting directly within Python applications.

![Python Create PDF](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5z4bgh4gfyzpo4q8kxln.png)

###Create a PDF File from a URL

The following example showcases how to use IronPDF in Python to convert a web page from a specified URL directly into a PDF file. By utilizing IronPDF's ChromePdfRenderer(), you can seamlessly capture and save online content as PDFs, ideal for archiving web pages or integrating web data into printable reports.

```
renderer = ChromePdfRenderer()

pdfFromUrl = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/PDF")
pdfFromUrl.SaveAs("website.pdf")

```
The above line of code uses IronPDF's ChromePdfRenderer() to fetch and convert the web page located at "https://en.wikipedia.org/wiki/PDF" into a PDF document (pdfFromUrl). This method is efficient for capturing and saving web content as PDF files directly within Python applications. In this way, we can easily create PDFs directly from the URL.

The output file is as:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zbadduqeog3z3v3okgjp.png)
###PDF Generation from HTML files

In this example, we showcase how IronPDF in Python can efficiently generate PDFs by converting the content of an HTML file into new PDF files. By leveraging ChromePdfRenderer(), this code demonstrates the process of creating PDFs directly from structured HTML content. This approach simplifies the task of transforming HTML files into professionally formatted PDFs, suitable for reports, presentations, and digital document archives. Below, we'll illustrate how to implement this functionality seamlessly within your Python projects. 

The Content of Sample HTML file is as:

```
<!DOCTYPE html>
<html>
<body>

<h1>Hello Worlds</h1>

<p>This is a sample HTML file for a PDF generation example.</p>

</body>
</html>
```
The Screenshot of the HTML file is:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fgkoepvjhvchhjkj6g3j.png)
Here's the code to convert an HTML file into a PDF using IronPDF:

```
renderer = ChromePdfRenderer()

pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf("sample_html_file.html")
pdfFromHtmlFile.SaveAs("html_file_to_pdf.pdf")
```

After executing this code, you can understand how it leverages IronPDF to convert the content of "sample_html_file.html" into a PDF document named "html_file_to_pdf.pdf".

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cc43xmuodglww4tv511m.png)
Now, let's explore advanced features by incorporating images, tables, and a watermark into our PDF file.

###Add Images and Table to PDF File:

Incorporating images, tables, or any element into a PDF file is straightforward. Simply include the desired elements in HTML format, then render the HTML to generate the PDF. This method ensures ease of integration and maintains clarity in your document creation process.

Here is the HTML content, I will use for adding images and table in pdf file. 

```
<!DOCTYPE html>
<html>
<body>

<h1>Hello Worlds</h1>

<p>This is a sample HTML file for a PDF generation example.</p>

<!DOCTYPE html>
<html>
<head>
<style>
table {
  border-collapse: collapse;
  width: 100%;
}

tr {
  border-bottom: 1px solid #ddd;
}
</style>
</head>
<body>

<h2>This is Sample Table with simple Design</h2>

<table>
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
  <th>Savings</th>
  </tr>
  <tr>
    <td>Peter</td>
    <td>Griffin</td>
    <td>$100</td>
  </tr>
  <tr>
    <td>Lois</td>
    <td>Griffin</td>
    <td>$150</td>
  </tr>
  <tr>
    <td>Joe</td>
    <td>Swanson</td>
    <td>$300</td>
  </tr>
  <tr>
    <td>Cleveland</td>
    <td>Brown</td>
    <td>$250</td>
  </tr>
</table>

<h2>Sample Image</h2>
<img src="C_Sharp.jpg" alt="C Sharp Logo" >

</body>
</html>
```

The screenshot of the above HTML file is as:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o9w6vbvukr9u4biwzz5v.png)
I will use the 'Generate PDF from HTML file' method for the advanced features to keep it simple. We will include HTML for tables and images to integrate them into our document. Using HTML as a string directly in Python code can make it messy, so for clarity and simplicity, I will use a separate HTML file to make the PDF generation process simple.

```
renderer = ChromePdfRenderer()

pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf("sample_html_file.html")
pdfFromHtmlFile.SaveAs("html_file_to_pdf.pdf")
```
As you can see, the code for generating PDFs remains the same. You only need to update your HTML to include any element in your PDF file.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cpkp4ks77471n887170k.png)
###Add Watermark to our Existing PDF file:

Here's how to enhance new PDF documents or existing PDF files by adding a watermark using IronPDF in Python. 

```
pdf = PdfDocument.FromFile("html_file_to_pdf.pdf")
pdf.ApplyWatermark("<h2 style='color:red'>SAMPLE WATERMARK</h2>", 30, VerticalAlignment.Top, HorizontalAlignment.Center)
pdf.SaveAs("html_file_to_pdf.pdf")
```

The above code snippet demonstrates how to load a PDF from a file ("html_file_to_pdf.pdf"), apply a watermark using HTML content `("<h2 style='color:red'>SAMPLE WATERMARK</h2>")`, and save the modified PDF with the watermark in place.

##Conclusion:

In conclusion, IronPDF offers robust capabilities for PDF conversion and management, enabling Python developers to create, edit, and manipulate documents with ease. Whether generating reports, incorporating encrypted PDF files, or embedding JPEG images, IronPDF simplifies Python-based PDF creation with its intuitive API and seamless integration. Developers can explore IronPDF's features with [a free trial](https://ironpdf.com/#trial-license) and opt for a [commercial license](https://ironpdf.com/licensing/) for continued use, ensuring efficient and effective PDF handling within their applications.