I have the following code
import pdfkit
options = {
'page-size': 'Letter',
'margin-top': '0.75in',
'margin-right': '0.75in',
'margin-bottom': '0.75in',
'margin-left': '0.75in',
'encoding': "UTF-8",
'custom-header' : [
('Accept-Encoding', 'gzip')
],
'cookie': [
('cookie-name1', 'cookie-value1'),
('cookie-name2', 'cookie-value2'),
],
'no-outline': None,
#'load-error-handling': 'ignore'
}
def textToPdf(source, destiny):
path_wkthmltopdf = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
pdfkit.from_file(source + '.txt', destiny, configuration=config, options=options)
I made some tests using files with 'txt' extension and it worked very well, but when I try with another extension such as 'php' I get the following error message:
Failed to load page
I thought it might be the file, but if I get the same 'txt' file and change the extension to php to work, to solve it I did the following trick:
def textToPdf(source, destiny):
os.rename(source, source + '.txt')
path_wkthmltopdf = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
pdfkit.from_file(source + '.txt', destiny, configuration=config, options=options)
os.rename(source + '.txt', source)
That works very well, it puts the 'txt' extension in the file and then removes it but did not want to change the original file, and whether or not it is unnecessary processing.
I'm using this script to go through folders and sub-folders, another problem when using this method of renaming is that if the script is finished in the middle of the process some files may have the extension txt.
How can I make pdfkit ignore extensions?
Update
def fileToString(file):
with open(file, 'r') as f:
return f.read().replace('\n', '<br/>')
return ''
def textToPdf(source, destiny):
path_wkthmltopdf = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
pdfkit.from_string(fileToString(source), destiny, configuration=config, options=options)
This also worked, converting the file to string first, but wanted to play the file directly so it did not have two renderings.
Another problem of doing this is because the formatting is lost, at the moment to solve I am creating a temporary file and the converting.
Update
To "solve" the problem I am creating a temporary file, I copy the original file to a folder with the extension "txt", convert, and send pdf to the destination with the right name.