Python configure pdfkit on windows

醉酒当歌 提交于 2019-12-03 22:58:50

It looks like you need to install wkhtmltopdf. For windows, the installer can be found at https://wkhtmltopdf.org/downloads.html

Also check out a post by this guy, who is having the same problem: Can't create pdf using python PDFKIT Error : " No wkhtmltopdf executable found:"

I found working solution. If you want to convert files to pdf format just don't use python for this purpose. You need to include DOMPDF library into your php script on your local/remove server. Something like this:

<?php
// include autoloader
require_once 'vendor/autoload.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;

if (isset($_POST['html']) && !empty($_POST['html'])) {
   // instantiate and use the dompdf class
   $dompdf = new Dompdf();
   $dompdf->loadHtml($_POST['html']);

   // (Optional) Setup the paper size and orientation
   $dompdf->setPaper('A4', 'landscape');

   // Render the HTML as PDF
   $dompdf->render();

   // Output the generated PDF to Browser
   $dompdf->stream();
} else {
   exit();
}

Then in your python script you can post your html or whatever content to your server and get generated pdf file as a response. Something like this:

import requests

url = 'http://example.com/html2pdf.php'
html = '<h1>hello</h1>'
r = requests.post(url, data={'html': html}, stream=True)

f = open('converted.pdf', 'wb')
f.write(r.content)
f.close()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!