{"id":13494184,"url":"https://github.com/JazzCore/python-pdfkit","last_synced_at":"2025-03-28T13:32:25.669Z","repository":{"id":6234853,"uuid":"7466540","full_name":"JazzCore/python-pdfkit","owner":"JazzCore","description":"Wkhtmltopdf python wrapper to convert html to pdf","archived":false,"fork":false,"pushed_at":"2023-10-26T11:22:53.000Z","size":165,"stargazers_count":1987,"open_issues_count":39,"forks_count":330,"subscribers_count":37,"default_branch":"master","last_synced_at":"2024-10-29T15:09:18.196Z","etag":null,"topics":[],"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/JazzCore.png","metadata":{"files":{"readme":"README.rst","changelog":"HISTORY.rst","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS.rst","dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-01-06T10:16:40.000Z","updated_at":"2024-10-25T16:07:01.000Z","dependencies_parsed_at":"2024-06-18T10:42:58.560Z","dependency_job_id":"7e9ed698-06fa-4859-8b95-ba349e7b6969","html_url":"https://github.com/JazzCore/python-pdfkit","commit_stats":{"total_commits":188,"total_committers":32,"mean_commits":5.875,"dds":"0.35106382978723405","last_synced_commit":"c83fa250f85b210f4a0f05ca613ec0fb9580732e"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JazzCore%2Fpython-pdfkit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JazzCore%2Fpython-pdfkit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JazzCore%2Fpython-pdfkit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JazzCore%2Fpython-pdfkit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JazzCore","download_url":"https://codeload.github.com/JazzCore/python-pdfkit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222149195,"owners_count":16939265,"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":[],"created_at":"2024-07-31T19:01:22.685Z","updated_at":"2024-10-31T08:31:58.298Z","avatar_url":"https://github.com/JazzCore.png","language":"Python","readme":"Python-PDFKit: HTML to PDF wrapper\n==================================\n\n\n.. image:: https://github.com/JazzCore/python-pdfkit/actions/workflows/main.yaml/badge.svg?branch=master\n        :target: https://github.com/JazzCore/python-pdfkit/actions/workflows/main.yaml\n\n.. image:: https://badge.fury.io/py/pdfkit.svg\n        :target: http://badge.fury.io/py/pdfkit\n\nPython 3 wrapper for wkhtmltopdf utility to convert HTML to PDF using Webkit.\n\nThis is adapted version of `ruby PDFKit \u003chttps://github.com/pdfkit/pdfkit\u003e`_ library, so big thanks to them!\n\nDeprecation Warning\n-------------------\n\nThis library has been deprecated to match the `wkhtmltopdf project status \u003chttps://wkhtmltopdf.org/status.html\u003e`_.\n\nInstallation\n------------\n\n1. Install python-pdfkit:\n\n.. code-block:: bash\n\n\t$ pip install pdfkit\n\n2. Install wkhtmltopdf:\n\n* Debian/Ubuntu:\n\n.. code-block:: bash\n\n\t$ sudo apt-get install wkhtmltopdf\n\n* macOS:\n\n.. code-block:: bash\n\n\t$ brew install homebrew/cask/wkhtmltopdf\n\n**Warning!** Version in debian/ubuntu repos have reduced functionality (because it compiled without the wkhtmltopdf QT patches), such as adding outlines, headers, footers, TOC etc. To use this options you should install static binary from `wkhtmltopdf \u003chttp://wkhtmltopdf.org/\u003e`_ site or you can use `this script \u003chttps://github.com/JazzCore/python-pdfkit/blob/master/ci/before-script.sh\u003e`_ (written for CI servers with Ubuntu 18.04 Bionic, but it could work on other Ubuntu/Debian versions).\n\n* Windows and other options: check wkhtmltopdf `homepage \u003chttp://wkhtmltopdf.org/\u003e`_ for binary installers\n\nUsage\n-----\n\nFor simple tasks:\n\n.. code-block:: python\n\n\timport pdfkit\n\n\tpdfkit.from_url('http://google.com', 'out.pdf')\n\tpdfkit.from_file('test.html', 'out.pdf')\n\tpdfkit.from_string('Hello!', 'out.pdf')\n\nYou can pass a list with multiple URLs or files:\n\n.. code-block:: python\n\n\tpdfkit.from_url(['google.com', 'yandex.ru', 'engadget.com'], 'out.pdf')\n\tpdfkit.from_file(['file1.html', 'file2.html'], 'out.pdf')\n\nAlso you can pass an opened file:\n\n.. code-block:: python\n\n    with open('file.html') as f:\n        pdfkit.from_file(f, 'out.pdf')\n\nIf you wish to further process generated PDF, you can read it to a variable:\n\n.. code-block:: python\n\n    # Without output_path, PDF is returned for assigning to a variable\n    pdf = pdfkit.from_url('http://google.com')\n\nYou can specify all wkhtmltopdf `options \u003chttp://wkhtmltopdf.org/usage/wkhtmltopdf.txt\u003e`_. You can drop '--' in option name. If option without value, use *None, False* or *''* for dict value:. For repeatable options (incl. allow, cookie, custom-header, post, postfile, run-script, replace) you may use a list or a tuple. With option that need multiple values (e.g. --custom-header Authorization secret) we may use a 2-tuple (see example below).\n\n.. code-block:: python\n\n\toptions = {\n\t    'page-size': 'Letter',\n\t    'margin-top': '0.75in',\n\t    'margin-right': '0.75in',\n\t    'margin-bottom': '0.75in',\n\t    'margin-left': '0.75in',\n\t    'encoding': \"UTF-8\",\n\t    'custom-header': [\n\t    \t('Accept-Encoding', 'gzip')\n\t    ],\n\t    'cookie': [\n\t    \t('cookie-empty-value', '\"\"')\n\t    \t('cookie-name1', 'cookie-value1'),\n\t    \t('cookie-name2', 'cookie-value2'),\n\t    ],\n\t    'no-outline': None\n\t}\n\n\tpdfkit.from_url('http://google.com', 'out.pdf', options=options)\n\nBy default, PDFKit will run ``wkhtmltopdf`` with ``quiet`` option turned on, since in most cases output is not needed and can cause excessive memory usage and corrupted results. If need to get ``wkhtmltopdf`` output you should pass ``verbose=True`` to API calls:\n\n.. code-block:: python\n\n    pdfkit.from_url('google.com', 'out.pdf', verbose=True)\n\nDue to wkhtmltopdf command syntax, **TOC** and **Cover** options must be specified separately. If you need cover before TOC, use ``cover_first`` option:\n\n.. code-block:: python\n\n\ttoc = {\n\t    'xsl-style-sheet': 'toc.xsl'\n\t}\n\n\tcover = 'cover.html'\n\n\tpdfkit.from_file('file.html', options=options, toc=toc, cover=cover)\n\tpdfkit.from_file('file.html', options=options, toc=toc, cover=cover, cover_first=True)\n\nYou can specify external CSS files when converting files or strings using *css* option.\n\n**Warning** This is a workaround for `this bug \u003chttp://code.google.com/p/wkhtmltopdf/issues/detail?id=144\u003e`_ in wkhtmltopdf. You should try *--user-style-sheet* option first.\n\n.. code-block:: python\n\n    # Single CSS file\n    css = 'example.css'\n    pdfkit.from_file('file.html', options=options, css=css)\n\n    # Multiple CSS files\n    css = ['example.css', 'example2.css']\n    pdfkit.from_file('file.html', options=options, css=css)\n\nYou can also pass any options through meta tags in your HTML:\n\n.. code-block:: python\n\n\tbody = \"\"\"\n\t    \u003chtml\u003e\n\t      \u003chead\u003e\n\t        \u003cmeta name=\"pdfkit-page-size\" content=\"Legal\"/\u003e\n\t        \u003cmeta name=\"pdfkit-orientation\" content=\"Landscape\"/\u003e\n\t      \u003c/head\u003e\n\t      Hello World!\n\t      \u003c/html\u003e\n\t    \"\"\"\n\n\tpdfkit.from_string(body, 'out.pdf') #with --page-size=Legal and --orientation=Landscape\n\nConfiguration\n-------------\n\nEach API call takes an optional configuration parameter. This should be an instance of ``pdfkit.configuration()`` API call. It takes the configuration options as initial parameters. The available options are:\n\n* ``wkhtmltopdf`` - the location of the ``wkhtmltopdf`` binary. By default ``pdfkit`` will attempt to locate this using ``which`` (on UNIX type systems) or ``where`` (on Windows).\n* ``meta_tag_prefix`` - the prefix for ``pdfkit`` specific meta tags - by default this is ``pdfkit-``\n\nExample - for when ``wkhtmltopdf`` is not on ``$PATH``:\n\n.. code-block:: python\n\n    config = pdfkit.configuration(wkhtmltopdf='/opt/bin/wkhtmltopdf')\n    pdfkit.from_string(html_string, output_file, configuration=config)\n\nAlso you can use ``configuration()`` call to check if wkhtmltopdf is present in ``$PATH``:\n\n.. code-block:: python\n\n\ttry:\n\t  config = pdfkit.configuration()\n\t  pdfkit.from_string(html_string, output_file)\n\texcept OSError:\n\t  #not present in PATH\n\n\nTroubleshooting\n---------------\n\nDebugging issues with PDF generation\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nIf you struggling to generate correct PDF firstly you should check ``wkhtmltopdf`` output for some clues, you can get it by passing ``verbose=True`` to API calls:\n\n.. code-block:: python\n\n\tpdfkit.from_url('http://google.com', 'out.pdf', verbose=True)\n\nIf you are getting strange results in PDF or some option looks like its ignored you should try to run ``wkhtmltopdf`` directly to see if it produces the same result. You can get CLI command by creating ``pdfkit.PDFKit`` class directly and then calling its ``command()`` method:\n\n.. code-block:: python\n\n\timport pdfkit\n\n\tr = pdfkit.PDFKit('html', 'string', verbose=True)\n\tprint(' '.join(r.command()))\n\t# try running wkhtmltopdf to create PDF\n\toutput = r.to_pdf()\n\nCommon errors:\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n- ``IOError: 'No wkhtmltopdf executable found'``:\n\n  Make sure that you have wkhtmltopdf in your `$PATH` or set via custom configuration (see preceding section). *where wkhtmltopdf* in Windows or *which wkhtmltopdf* on Linux should return actual path to binary.\n\n- ``IOError: 'Command Failed'``\n\n  This error means that PDFKit was unable to process an input. You can try to directly run a command from error message and see what error caused failure (on some wkhtmltopdf versions this can be cause by segmentation faults)\n","funding_links":[],"categories":["Python","PYTHON","📚 فهرست"],"sub_categories":["کار با pdf"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FJazzCore%2Fpython-pdfkit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FJazzCore%2Fpython-pdfkit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FJazzCore%2Fpython-pdfkit/lists"}