Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/navin-mohan/pychromepdf
Creates PDFs from HTML rendered using chrome or chromium
https://github.com/navin-mohan/pychromepdf
chrome-headless flask html pdf pdf-generation python
Last synced: 1 day ago
JSON representation
Creates PDFs from HTML rendered using chrome or chromium
- Host: GitHub
- URL: https://github.com/navin-mohan/pychromepdf
- Owner: navin-mohan
- License: mit
- Created: 2019-02-04T16:27:06.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-11-26T21:29:28.000Z (about 2 months ago)
- Last Synced: 2025-01-12T14:45:15.105Z (3 days ago)
- Topics: chrome-headless, flask, html, pdf, pdf-generation, python
- Language: Python
- Size: 27.3 KB
- Stars: 25
- Watchers: 1
- Forks: 5
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Pychromepdf [![PyPI version](https://badge.fury.io/py/pychromepdf.png)](https://badge.fury.io/py/pychromepdf) [![Travis build status](https://travis-ci.org/navin-mohan/pychromepdf.svg?branch=master)](https://travis-ci.org/github/navin-mohan/pychromepdf) [![Downloads](https://pepy.tech/badge/pychromepdf/month)](https://pepy.tech/project/pychromepdf)
Pychromepdf is a Python package that lets you easily create PDFs by rendering HTML content using Chrome or Chromium as backend. It works without any external dependecies except a working installation of Chrome or Chromium that supports headless mode.
# Installation
```bash
pip install pychromepdf
```## Usage
### Rendering HTML bytestring to PDF
```python
from pychromepdf import ChromePDF# change to your chrome executable path
PATH_TO_CHROME_EXE = '/usr/bin/google-chrome-stable'
# if you're on MacOS
# PATH_TO_CHROME_EXE = '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome'if __name__ == '__main__':
# initialize chromepdf object
cpdf = ChromePDF(PATH_TO_CHROME_EXE)# the html that need to be rendered into pdf
html_bytestring = '''
@media print {
@page { margin: 0; }
body { margin: 1.6cm; }
}
Hello, World
Generated using headless chrome
'''# create a file and write the pdf to it
with open('test.pdf','w') as output_file:
if cpdf.html_to_pdf(html_bytestring,output_file):
print("Successfully generated the pdf: {}".format(output_file.name))
else:
print("Error generating pdf")```
### Rendering a flask template into PDF
```python
from flask import Flask, render_template, send_file
import tempfile
from pychromepdf import ChromePDFapp = Flask(__name__)
# change to your chrome executable path
PATH_TO_CHROME_EXE = '/usr/bin/google-chrome-stable'
# if you're on MacOS
# PATH_TO_CHROME_EXE = '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome'# initialize a chromepdf object
cpdf = ChromePDF(PATH_TO_CHROME_EXE)# home route
@app.route('/')
def index():
return render_template('index.html',username="John")# custom pdf route
@app.route('/getpdf',defaults={'username': 'John'})
@app.route('/getpdf/')
def getpdf(username):# get the rendered html as string using the template
rendered_html = render_template('index.html',username=username)# create a temporary output file which will be deleted when closed
with tempfile.NamedTemporaryFile(suffix='.pdf') as output_file:# create a pdf from the rendered html and write it to output_file
if cpdf.html_to_pdf(rendered_html,output_file):
print("PDF generated successfully: {0}".format(output_file.name))try:
# send the file to user
return send_file(output_file.name,attachment_filename='awesome.pdf')
except Exception as e:
return str(e)
else:
print("Error creating PDF")return "Error"
if __name__ == '__main__':
app.run(debug=True)```
Template
```html
{# templates/index.html #}
Example
@media print {
@page { margin: 0; }
body { margin: 1.6cm; }
}
Hello {{ username }}!
Generated using ChromePDF
```
# Contributors
- [nvnmo](https://github.com/navin-mohan)
- [chibiegg](https://github.com/chibiegg)# License
MIT License