{"id":20619676,"url":"https://github.com/twtrubiks/python-pdfkit-example","last_synced_at":"2025-07-10T01:33:13.299Z","repository":{"id":84518985,"uuid":"78921140","full_name":"twtrubiks/python-pdfkit-example","owner":"twtrubiks","description":"python-pdfkit HTML TO PDF Example","archived":false,"fork":false,"pushed_at":"2017-01-14T10:13:12.000Z","size":13749,"stargazers_count":20,"open_issues_count":0,"forks_count":14,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-15T11:55:07.376Z","etag":null,"topics":["flask","html-to-pdf","python","python-pdfkit","tutorial","wkhtmltopdf"],"latest_commit_sha":null,"homepage":null,"language":"C","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/twtrubiks.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-01-14T06:36:57.000Z","updated_at":"2024-06-26T12:48:26.000Z","dependencies_parsed_at":"2023-03-02T04:30:27.322Z","dependency_job_id":null,"html_url":"https://github.com/twtrubiks/python-pdfkit-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2Fpython-pdfkit-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2Fpython-pdfkit-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2Fpython-pdfkit-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2Fpython-pdfkit-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/twtrubiks","download_url":"https://codeload.github.com/twtrubiks/python-pdfkit-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249067775,"owners_count":21207395,"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":["flask","html-to-pdf","python","python-pdfkit","tutorial","wkhtmltopdf"],"created_at":"2024-11-16T12:12:15.881Z","updated_at":"2025-04-15T11:55:09.011Z","avatar_url":"https://github.com/twtrubiks.png","language":"C","readme":"# python-pdfkit-example\npython-pdfkit HTML TO PDF Example\n\n使用 python-pdfkit 將 HTML 轉成 PDF\n \n* [Youtube Demo](https://youtu.be/ceIJRWriTig)  \n \n常常需要將一些東西轉換成 PDF，今天教你使用 [python-pdfkit](https://github.com/JazzCore/python-pdfkit) 快速完成這個功能。\n\n使用 Python [Flask](http://flask.pocoo.org/) 搭配 [python-pdfkit](https://github.com/JazzCore/python-pdfkit)  實現轉換成 PDF 功能。\n\n## 特色\n* 搭配 [python-pdfkit](https://github.com/JazzCore/python-pdfkit) 實現轉換成 PDF 功能 ( HTML 轉成 PDF)。\n\n\n## 安裝套件 \n請先確定電腦有安裝 [Python](https://www.python.org/)\n\n因為轉出 PDF 是用到 [python-pdfkit](https://github.com/JazzCore/python-pdfkit)  以及 [wkhtmltopdf](http://wkhtmltopdf.org/)\n\n所以必須安裝這兩個套件，以下用 Windows 環境介紹\n\n### python-pdfkit\n``` \npip install pdfkit\n```\n更多 python-pdfkit ，可參考  [python-pdfkit](https://github.com/JazzCore/python-pdfkit) \n\n### wkhtmltopdf\n最後 python 還是會去使用 wkhtmltopdf.exe ，所以請務必安裝 [wkhtmltopdf](http://wkhtmltopdf.org/)\n\n安裝方法很簡單，基本上就是無腦安裝 exe 檔(請選擇符合自己電腦的版本)，請記好自己安裝的路徑\n\n![alt tag](http://i.imgur.com/t13xYR5.png)\n\n![alt tag](http://i.imgur.com/v7N4vIN.png)\n\n安裝好 wkhtmltopdf 之後，建議大家可以先做測試，測試方法如下\n\n先使用 cmd (命令提示字元) 切換到 wkhtmltopdf 路徑下 \n\n接著請在  cmd (命令提示字元) 輸入以下指令\n\n``` \nwkhtmltopdf \"https://www.google.com.tw/\" out.pdf\n```\n![alt tag](http://i.imgur.com/IbsxiDn.png)\n\n順利的話，路徑下會多出 out.pdf 的檔案，打開它可以看到我們將 google 的首頁轉成 pdf 文件了\n\n![alt tag](http://i.imgur.com/Mku3y4X.png)\n\n更多 wkhtmltopdf ，可參考  [wkhtmltopdf](http://wkhtmltopdf.org/)\n\n\n## 開始使用 python-pdfkit\n\n### 設定路徑\n\n前面有提到  python-pdfkit 還是會去使用 wkhtmltopdf.exe，所以我們必須告訴系統自己的 wkhtmltopdf.exe 路徑\n\n``` \nconfig = pdfkit.configuration(wkhtmltopdf='wkhtmltopdf.exe 存在路徑')\npdfkit.from_url(\"目標網址\", \"輸出檔案\", configuration=config)\n```\n``` \nconfig = pdfkit.configuration(wkhtmltopdf='C:/Program Files (x86)/wkhtmltopdf/bin/wkhtmltopdf.exe')\npdfkit.from_url(\"https://www.google.com.tw/\", \"out.pdf\", configuration=config)\n```\n當然，你也可以直接設定電腦的環境變數。\n\n### 一些 python-pdfkit 基本的使用\n\n``` \nimport pdfkit\n\n# 指定目標網址 轉成 pdf \npdfkit.from_url('https://www.google.com.tw/', 'out.pdf')\n\n# 指定目標檔案(html) 轉成 pdf \npdfkit.from_file('test.html', 'out.pdf')\n\n# 指定目標字串 轉成 pdf \npdfkit.from_string('Hello!', 'out.pdf')\n```\n\n指定目標字串也可以寫 html\n\n``` \nbody = \"\"\"\n    \u003chtml\u003e\n      \u003chead\u003e\n        \u003cmeta name=\"pdfkit-page-size\" content=\"Legal\"/\u003e\n        \u003cmeta name=\"pdfkit-orientation\" content=\"Landscape\"/\u003e\n      \u003c/head\u003e\n      Hello World!\n      \u003c/html\u003e\n    \"\"\"\n\npdfkit.from_string(body, 'out.pdf')\n``` \n\n合併多的網址、檔案(html) 轉成 pdf \n\n``` \npdfkit.from_url(['https://www.google.com.tw/', 'https://tw.yahoo.com/', 'https://shopping.pchome.com.tw/'], 'out.pdf')\npdfkit.from_file(['file1.html', 'file2.html'], 'out.pdf')\n``` \n\n額外多的設定，像是邊距、邊碼、大小\n``` \noptions = {\n    'page-size': 'Letter',\n    'margin-top': '0.75in',\n    'margin-right': '0.75in',\n    'margin-bottom': '0.75in',\n    'margin-left': '0.75in',\n    'encoding': \"UTF-8\",\n    'custom-header' : [\n        ('Accept-Encoding', 'gzip')\n    ]\n    'cookie': [\n        ('cookie-name1', 'cookie-value1'),\n        ('cookie-name2', 'cookie-value2'),\n    ],\n    'no-outline': None\n}\n\npdfkit.from_url('https://www.google.com.tw/', 'out.pdf', options=options)\n``` \n更多的額外設定的參數，可參考這份說明 [wkhtmltopdf.txt](http://wkhtmltopdf.org/usage/wkhtmltopdf.txt)\n\nP.S \u003cbr\u003e\n如果出現如下圖(很多黑色框框)，代表你的電腦可能沒安裝字型檔\n![alt tag](http://i.imgur.com/RY6QT6y.png)\n\n如果出現亂碼(如下圖)，代表是編碼問題\n\n![alt tag](http://i.imgur.com/QIxMIw9.png)\n\n\n指定編碼即可解決這個問題\n``` \noptions = {\n        'encoding': 'UTF-8'\n    }\npdfkit.from_string(body, filename, configuration=config, options=options)\n```\n![alt tag](http://i.imgur.com/q0OGpv9.png)\n\n如果出現下方的錯誤\n``` \nIOError: 'No wkhtmltopdf executable found'\n```\n通常是你沒安裝 wkhtmltopdf 或是 你忘記指定  wkhtmltopdf.exe 的路徑\n\n\n\n## 執行畫面 (簡單範例)\n\n執行程式，請用 \u003cb\u003e系統管理員\u003c/b\u003e 執行 (不然有時候會無法存取)\n\n請將記得安裝套件\n``` \npip install -r requirements.txt\n``` \n\n\n首頁\n![alt tag](http://i.imgur.com/kF3723e.jpg)\n可以輸入網址  (將目標網址轉成 PDF )\n\nPDF template (範例)\n![alt tag](http://i.imgur.com/4wLFq1x.jpg)\n \n## 執行環境\n* Python 3.4.3\n\n## Reference \n* [python-pdfkit](https://github.com/JazzCore/python-pdfkit/) \n* [wkhtmltopdf](http://wkhtmltopdf.org/)\n\n## License\nMIT license\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwtrubiks%2Fpython-pdfkit-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftwtrubiks%2Fpython-pdfkit-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwtrubiks%2Fpython-pdfkit-example/lists"}