Selchuk Hadzhaahmed
Selchuk Hadzhaahmed

Reputation: 3

Conversion HTML file to PDF with using wkhtmltopdf and pdfkit Encoding Problem on Spyder

I am trying to convert some Turkish HTML files to PDF files with using pdfkit module and wkhtmltopdf on Spyder. But I got encoding issues while converting.

When I try to run same code block on Jupyter Notebook, Turkish special letters convert perfectly. I use same Python environment on Spyder and Jupyter Notebook. And I set all encoding UFT-8 options on Spyder.

HTML files have also:

<html><head><META http-equiv="Content-Type" content="text/html; charset=utf-8"><style type="text/css">
          body, td, th, p, div, span {
          font-family: Arial, Helvetica, sans-serif;
          font-size: 10px;`
supporting turkish language meta tags (charset and font-family)

I did not understand why Spyder can not convert well the html files. You can find the code block below.

print(f"HTML Name: {files_data[i]}")
htmltopdf_path = r'C:\Tools\wkhtmltopdf\bin\wkhtmltopdf.exe'  
config_kmp = pdfkit.configuration(wkhtmltopdf=htmltopdf_path)
options = {
      'encoding' : 'UTF-8'
        }
try:
    with open(rf"{pdf_html_files_path}" + '/' + f"{files_data[i]}") as f:
        pdfkit.from_file(f,"./test_folder/output.pdf", configuration=config_kmp, options = options)
except:
    print("HTML to PDF Conversion Failed")
    pass

Spyder converts like this; Spyder Conversion Result

Jupyter Notebook converts like;

Jupyter Notebook Conversion Result

Upvotes: 0

Views: 76

Answers (1)

Selchuk Hadzhaahmed
Selchuk Hadzhaahmed

Reputation: 3

The issue is solved. I added encoing = 'utf-8' on with statement while opening the file.

The code works without encoding parameter on Jupyter Notebook, but does not work on Spyder.

Upvotes: 0

Related Questions