---
title: "Python"
canonical_url: "https://www.nutrient.io/guides/document-engine/pdf-generation/from-html/python/"
md_url: "https://www.nutrient.io/guides/document-engine/pdf-generation/from-html/python.md"
last_updated: "2026-05-26T12:37:25.415Z"
description: "Learn how to create PDFs from HTML using .NET, Java, Python, PHP, and JavaScript with comprehensive guides and sample code for each language."
---

# Python

###.NET

[.NET](https://www.nutrient.io/guides/document-engine/pdf-generation/from-html/sample-code.md)

### Java

[Java](https://www.nutrient.io/guides/document-engine/pdf-generation/from-html/java.md)

### Python

[Python](https://www.nutrient.io/guides/document-engine/pdf-generation/from-html/python.md)

### JavaScript

[JavaScript](https://www.nutrient.io/guides/document-engine/pdf-generation/from-html/javascript.md)

### PHP

[PHP](https://www.nutrient.io/guides/document-engine/pdf-generation/from-html/php.md)

Using a templating language such as [Mustache](http://mustache.github.io/) allows you to inject your data at runtime. In regard to PDF Generation, this means it gives you the possibility to customize HTML before sending it along for generation. Injecting data can be useful in situations such as name and date replacement, or when generating dynamic lists in invoices.

In this guide, you’ll implement name and date replacement with the use of Mustache in Python. Note `{{name}}` and `{{date}}` in the following HTML:

```html

<!DOCTYPE html>
<html>
  <body>
    <div class="address">
      John Smith<br/>
      123 Smith Street<br/>
      90568 TA<br/>
      <br/>
      {{date}}
    </div>
    <div class="subject">Subject: PDF Generation FTW!</div>
    <div>
      <p>
        PDF is great!
      </p>
    </div>
    <div>{{name}}<br/></div>
  </body>
</html>

```



First, you need to replace your template data with something real. Define the data to replace the template arguments in code, though in practice, this data may come from an external source or database.

To perform the replacement, you’ll use the [Chevron](https://github.com/noahmorrison/chevron) package, which is an implementation of the [Mustache](http://mustache.github.io/) templating system.

The following will stream the `page.mustache` file (the HTML seen above) into Chevron to perform the replacement of `{{name}}` and `{{date}}`:

```python

with open('page.mustache', 'r') as f:
    final_html = chevron.render(f, {'name': 'John Smith Jr.', "date": "29 February, 2020"})

```

Once you’ve created your HTML in Python, send a request to the [`/api/documents`](https://www.nutrient.io/api/reference/document-engine/upstream/#tag/Documents/operation/upload-document) endpoint, sending the [PDF Generation schema](https://www.nutrient.io/guides/document-engine/pdf-generation/from-html/schema.md) with the HTML file you just created:

### SHELL

```shell

curl -X POST http://localhost:5000/api/documents \
  -H "Authorization: Token token=<API token>" \
  -F page.html=@/path/to/page.html \
  -F generation='{
  "html": "page.html"
}'

```

### HTTP

```http

POST /api/documents HTTP/1.1
Content-Type: multipart/form-data; boundary=customboundary
Authorization: Token token=<API token>

--customboundary
Content-Disposition: form-data; name="page.html"; filename="page.html"
Content-Type: text/html

<HTML data>
--customboundary
Content-Disposition: form-data; name="generation"
Content-Type: application/json

{
  "html": "page.html"
}
--customboundary--

```





## Adding watermarks

Once you have the basics of PDF generation down, you might want to watermark your PDF. You can do this by including some additional HTML:

```html

<!DOCTYPE html>
<html>
  <body>
    <div style="position: fixed;
      top: 50%;
      left: 50%;
      font-size: 72px;
      color: red;
      opacity: 80%;
      transform: rotate(-45deg);
      width: 500px;
      height: 500px;
      margin-top: -250px;
      margin-left: -250px;
      text-align: center;
      vertical-align: middle;
      line-height: 500px;">
      My Watermark
    </div>
    <div class="address">
      John Smith<br/>
      123 Smith Street<br/>
      90568 TA<br/>
      <br/>
      {{date}}
    </div>
    <div class="subject">Subject: PDF Generation FTW!</div>
    <div>
      <p>
        PDF is great!
      </p>
    </div>
    <div>{{name}}<br/></div>
  </body>
</html>

```

This element will be rendered on all pages on top of all other content. The HTML above uses some text, but you could also use an image. The watermark can also be positioned any way you want.![A PDF with a watermark](@/assets/guides/document-engine/pdf-generation/watermark-document.png)

## Adding a cover page

There are two ways to add a cover page to a PDF you generated: You can generate the cover page as part of the HTML, or you can use a PDF you already have.

### Using HTML

If you want to add a cover page to your generated PDF, you can do so by adding additional HTML:

```html

<!DOCTYPE html>
<html>
  <body>
    <div style="display: block; width: 100%; height: 100%; page-break-after: always;">
      <h1>My Cover Page</h1>
    </div>
    <div class="address">
      John Smith<br/>
      123 Smith Street<br/>
      90568 TA<br/>
      <br/>
      {{date}}
    </div>
    <div class="subject">Subject: PDF Generation FTW!</div>
    <div>
      <p>
        PDF is great!
      </p>
    </div>
    <div>{{name}}<br/></div>
  </body>
</html>

```

This will add another page before the rest of the content, and you can use the new page as your cover page.![A PDF with a cover page](@/assets/guides/document-engine/pdf-generation/cover-page-document.png)

### Using a PDF

Instead of adding some HTML as a cover page, you can add a PDF. This can be done by applying an `importDocument` operation on upload:

### SHELL

```shell

curl -X POST http://localhost:5000/api/documents \
  -H "Authorization: Token token=<API token>" \
  -F page.html=@/path/to/page.html \
  -F cover.pdf=@/path/to/cover.pdf \
  -F generation='{
  "html": "page.html"
}' \
  -F operations='{
  "operations": [
    {
      "type": "importDocument",
      "beforePageIndex": 0,
      "document": "cover.pdf"
    }
  ]
}'

```

### HTTP

```http

POST /api/documents HTTP/1.1
Content-Type: multipart/form-data; boundary=customboundary
Authorization: Token token=<API token>

--customboundary
Content-Disposition: form-data; name="page.html"; filename="page.html"
Content-Type: text/html

<HTML data>
--customboundary
Content-Disposition: form-data; name="cover.pdf"; filename="cover.pdf"
Content-Type: application/pdf

<PDF data>
--customboundary
Content-Disposition: form-data; name="generation"
Content-Type: application/json

{
  "html": "page.html"
}
--customboundary
Content-Disposition: form-data; name="operations"
Content-Type: application/json

{
  "operations": [
    {
      "type": "importDocument",
      "beforePageIndex": 0,
      "document": "cover.pdf"
    }
  ]
}
--customboundary--

```

The provided PDF will be appended before the first page of the HTML, and it’ll serve as your cover page.
---

## Related pages

- [Java](/guides/document-engine/pdf-generation/from-html/java.md)
- [Edit a generated PDF](/guides/document-engine/pdf-generation/from-html/edit-a-generated-pdf.md)
- [Generate a blank PDF](/guides/document-engine/pdf-generation/from-html/blank-pdf.md)
- [JavaScript](/guides/document-engine/pdf-generation/from-html/javascript.md)
- [Create PDFs from scratch](/guides/document-engine/pdf-generation/from-html/from-scratch.md)
- [Customize the page header and footer](/guides/document-engine/pdf-generation/from-html/page-header-footer.md)
- [PHP](/guides/document-engine/pdf-generation/from-html/php.md)
- [HTML-to-PDF conversion server sample code](/guides/document-engine/pdf-generation/from-html/sample-code.md)
- [HTML template design for generating PDFs](/guides/document-engine/pdf-generation/from-html/template-design.md)
- [HTML-to-PDF generation schema](/guides/document-engine/pdf-generation/from-html/schema.md)
- [Generate fillable PDF forms from HTML](/guides/document-engine/pdf-generation/from-html/fillable-pdf-forms.md)

