簡體   English   中英

如何在 Windows 10 中安裝 pdfkit(python 模塊)?

[英]How to install pdfkit (python module) in windows 10?

我使用 pip ( pip install pdfkit ) pip install pdfkit

然后我從這里安裝 wkhtmltopdf

但是當我嘗試運行以下代碼時:

import pdfkit

config = pdfkit.configuration(wkhtmltopdf='C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe')
pdfkit.from_url('https://www.google.com', 'C:\\Users\\Χρήστος\\Desktop\\out-test.pdf', configuration=config)

錯誤 oqqurs:

Traceback (most recent call last):
  File "create_pdf.py", line 4, in <module>
    pdfkit.from_url('https://www.google.com', 'C:\\Users\\Χρήστος\\Desktop\\out-test.pdf', configuration=config)
  File "C:\Users\Χρήστος\AppData\Local\Programs\Python\Python38\lib\site-packages\pdfkit\api.py", line 26, in from_url
    return r.to_pdf(output_path)
  File "C:\Users\Χρήστος\AppData\Local\Programs\Python\Python38\lib\site-packages\pdfkit\pdfkit.py", line 156, in to_pdf
    raise IOError('wkhtmltopdf reported an error:\n' + stderr)
OSError: wkhtmltopdf reported an error:
Loading pages (1/6)
QPainter::begin(): Returned false============================] 100%
Error: Unable to write to destination
Exit with code 1, due to unknown error.

任何建議都會有用。

我還使用管理權限運行代碼。

編輯:來自 wkhtmltopdf 站點:

我如何使用它? 下載預編譯的二進制文件或從源代碼構建 創建您想要轉換為 PDF(或圖像)的 HTML 文檔 通過該工具運行您的 HTML 文檔。 例如,如果我真的很喜歡 Google 今天對其徽標所做的處理,並希望將其永久捕獲為 PDF:

wkhtmltopdf http://google.com google.pdf

所以我試過這個:

cd C:\Program Files\wkhtmltopdf\bin
wkhtmltopdf http://google.com google.pdf

沒有用,但是當我使用管理權限運行它時,pdf 就生成了。 因此 pdfkit 模塊必須以管理員權限打開 wkhtmltopdf。

GitHub 上有問題,看來您必須指定完整路徑(例如“C://foo/bar”)

可能是因為腳本沒有權限在它所在的文件夾中寫入:
C:\\Users\\Χρήστος\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages\\pdfkit\\pdfkit.py

你給了它一個相對路徑,所以在運行時,它會嘗試創建一個文件:
C:\\Users\\Χρήστος\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages\\pdfkit\\out.pdf

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM