How do I make pdfkit ignore extensions?

Asked

Viewed 114 times

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?

Complete code

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.

1 answer

0

Most likely the pdfkit.from_file is using some function of "run the file" from the operating system instead of simply reading the file.

The documentation says that you can pass, instead of a file name, an already opened file - then it will probably only use the method read of the parameter passed:

pdfkit.from_file(open(source), destiny, configuration=config, options=options)

If the accent is not correct, pass the appropriate parameter to the open (ex. open(source, encoding='utf-8'))

  • I tested here, this displaying the following error message '_io.Textiowrapper' Object has no attribute 'NCODE'

  • I’m trying to create a function to read the file and convert to string, but it doesn’t grab the line breaks

  • I managed to do the line break using '<br>' in place of ' n', but wanted the way you did it to work

  • Putting this way pdfkit.from_file(open(source, 'r'), Destiny, Configuration=config, options=options), the code runs, it converts, but misses the formation.

  • Because of the first mistake - try for "rb" instead of "r".

  • I tried, and presented the following error "bytes' Object has in the attribute 'Encode'"

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.