0
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 did some tests using files with extension 'txt' and worked very well, but when I try with another extension like 'php' from the following error message:
Failed loading page
I thought it might be the file, but if I took the same file 'txt' and changed the extension to php to work, to solve I did the following gambiarra:
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 extension 'txt' in the file and then removes, but did not want to change the original file, and whether or not it is unnecessary processing.
I am using this script to go through folders and sub-folders, another problem when using this rename method is that if the script is finished in the middle of the process some file may stick to the txt extension.
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 way also worked, before converting the file to string, but wanted to play the file directly not to have two processing.
Another problem to do this way is because 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, copy the original file to a folder with the extension "txt", convert, and send pdf to the destination with the right name.
I tested here, this displaying the following error message '_io.Textiowrapper' Object has no attribute 'NCODE'
– Wictor Chaves
I’m trying to create a function to read the file and convert to string, but it doesn’t grab the line breaks
– Wictor Chaves
I managed to do the line break using '<br>' in place of ' n', but wanted the way you did it to work
– Wictor Chaves
Putting this way pdfkit.from_file(open(source, 'r'), Destiny, Configuration=config, options=options), the code runs, it converts, but misses the formation.
– Wictor Chaves
Because of the first mistake - try for
"rb"
instead of"r"
.– jsbueno
I tried, and presented the following error "bytes' Object has in the attribute 'Encode'"
– Wictor Chaves