1

I started to learn python recently and I want to convert existing html file to pdf file. It is very strange, but pdfkit seems to be the only lib for pdf docs for python.

import pdfkit
pdfkit.from_file("C:\\Users\\user\Desktop\\table.html", "out.pdf")

An error occurs: OSError: No wkhtmltopdf executable found: "b''"

How to configure this lib properly on windows to make it work? I can't get it :(

mr.boris
  • 3,667
  • 8
  • 37
  • 70
  • pdfkit is not the only option for generating PDF from Python. [Sphinx](http://www.sphinx-doc.org) is a very powerful documentation generator and can output PDF using LaTeX, [rinohtype](http://www.mos6581.org/rinohtype/) or [rst2pdf](http://rst2pdf.ralsina.me) (alongside many other formats. There are also some other Python packages that can produce PDF from HTML, such as [Weasyprint](http://weasyprint.org). – Brecht Machiels Jun 23 '17 at 15:40

2 Answers2

0

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:"

Sean
  • 467
  • 6
  • 12
  • 1
    Thank you. Last time I tried to install wkhtmltopdf via pip and it did not work. After a large number of attempts these lines of code (precisely) solve the problem `config = pdfkit.configuration(wkhtmltopdf="C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe") pdfkit.from_url('http://google.com', 'out.pdf', configuration=config)` IT IS SO WIRED! How do you even code with python? :) – mr.boris Jun 21 '17 at 02:13
  • 2
    Oh no. Pdfkit works terribly. After some testing It turned out that some html files can't be converted properly by pdfkit lib. As an output it gives empty tables and so on... Just why there is no working lib for pdf processing??? – mr.boris Jun 21 '17 at 02:34
0

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()
mr.boris
  • 3,667
  • 8
  • 37
  • 70