{"id":24196274,"url":"https://github.com/navin-mohan/pychromepdf","last_synced_at":"2025-06-30T03:34:09.333Z","repository":{"id":62579269,"uuid":"169109112","full_name":"navin-mohan/pychromepdf","owner":"navin-mohan","description":"Creates PDFs from HTML rendered using chrome or chromium","archived":false,"fork":false,"pushed_at":"2024-11-26T21:29:28.000Z","size":28,"stargazers_count":25,"open_issues_count":5,"forks_count":5,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-18T11:17:47.658Z","etag":null,"topics":["chrome-headless","flask","html","pdf","pdf-generation","python"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/navin-mohan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-02-04T16:27:06.000Z","updated_at":"2024-11-26T21:28:02.000Z","dependencies_parsed_at":"2022-11-03T20:48:43.508Z","dependency_job_id":null,"html_url":"https://github.com/navin-mohan/pychromepdf","commit_stats":null,"previous_names":["nvnmo/pychromepdf"],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/navin-mohan/pychromepdf","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/navin-mohan%2Fpychromepdf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/navin-mohan%2Fpychromepdf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/navin-mohan%2Fpychromepdf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/navin-mohan%2Fpychromepdf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/navin-mohan","download_url":"https://codeload.github.com/navin-mohan/pychromepdf/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/navin-mohan%2Fpychromepdf/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260540988,"owners_count":23024906,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chrome-headless","flask","html","pdf","pdf-generation","python"],"created_at":"2025-01-13T19:24:20.338Z","updated_at":"2025-06-30T03:34:09.302Z","avatar_url":"https://github.com/navin-mohan.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"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)\n\nPychromepdf 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.\n\n# Installation\n\n```bash\npip install pychromepdf\n```\n\n## Usage\n\n### Rendering HTML bytestring to PDF\n\n```python\nfrom pychromepdf import ChromePDF\n\n# change to your chrome executable path\nPATH_TO_CHROME_EXE = '/usr/bin/google-chrome-stable'\n# if you're on MacOS\n# PATH_TO_CHROME_EXE = '/Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome'\n\nif __name__ == '__main__':\n    # initialize chromepdf object\n    cpdf = ChromePDF(PATH_TO_CHROME_EXE)\n\n    # the html that need to be rendered into pdf\n    html_bytestring = '''\n    \u003c!doctype html\u003e\n    \u003chtml\u003e\n        \u003chead\u003e\n            \u003cstyle\u003e\n            @media print {\n                @page { margin: 0; }\n                body { margin: 1.6cm; }\n            }\n            \u003c/style\u003e\n        \u003c/head\u003e\n        \u003cbody\u003e\n            \u003ch1\u003eHello, World\u003c/h1\u003e\n            \u003ch5\u003e Generated using headless chrome \u003c/h5\u003e\n        \u003c/body\u003e\n    \u003c/html\u003e\n    '''\n\n    # create a file and write the pdf to it\n    with open('test.pdf','w') as output_file:\n        if cpdf.html_to_pdf(html_bytestring,output_file):\n            print(\"Successfully generated the pdf: {}\".format(output_file.name))\n        else:\n            print(\"Error generating pdf\")\n\n```\n\n### Rendering a flask template into PDF\n\n```python\nfrom flask import Flask, render_template, send_file\nimport tempfile\nfrom pychromepdf import ChromePDF\n\napp = Flask(__name__)\n\n# change to your chrome executable path\nPATH_TO_CHROME_EXE = '/usr/bin/google-chrome-stable'\n# if you're on MacOS\n# PATH_TO_CHROME_EXE = '/Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome'\n\n# initialize a chromepdf object\ncpdf = ChromePDF(PATH_TO_CHROME_EXE)\n\n# home route\n@app.route('/')\ndef index():\n    return render_template('index.html',username=\"John\")\n\n# custom pdf route\n@app.route('/getpdf',defaults={'username': 'John'})\n@app.route('/getpdf/\u003cusername\u003e')\ndef getpdf(username):\n\n    # get the rendered html as string using the template\n    rendered_html = render_template('index.html',username=username)\n\n    # create a temporary output file which will be deleted when closed\n    with tempfile.NamedTemporaryFile(suffix='.pdf') as output_file:\n\n        # create a pdf from the rendered html and write it to output_file\n        if cpdf.html_to_pdf(rendered_html,output_file):\n            print(\"PDF generated successfully: {0}\".format(output_file.name))\n\n            try:\n                # send the file to user\n                return send_file(output_file.name,attachment_filename='awesome.pdf')\n            except Exception as e:\n                return str(e)\n        else:\n            print(\"Error creating PDF\")\n\n    return \"Error\"\n                \n\nif __name__ == '__main__':\n    app.run(debug=True)\n\n```\n\nTemplate\n\n```html\n{# templates/index.html #}\n\n\u003c!DOCTYPE html\u003e\n\u003chtml\u003e\n\u003chead\u003e\n    \u003cmeta charset=\"utf-8\" /\u003e\n    \u003cmeta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"\u003e\n    \u003ctitle\u003eExample\u003c/title\u003e\n    \u003cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1\"\u003e\n    \u003cstyle\u003e\n        @media print {\n            @page { margin: 0; }\n            body { margin: 1.6cm; }\n        }\n    \u003c/style\u003e    \n\u003c/head\u003e\n\u003cbody\u003e\n    \u003ch1\u003eHello {{ username }}!\u003c/h1\u003e\n    \u003ch4\u003eGenerated using ChromePDF\u003c/h4\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n\n```\n# Contributors\n- [nvnmo](https://github.com/navin-mohan)\n- [chibiegg](https://github.com/chibiegg)\n\n# License\nMIT License\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnavin-mohan%2Fpychromepdf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnavin-mohan%2Fpychromepdf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnavin-mohan%2Fpychromepdf/lists"}