wkhtmltopdf installation issue, maybe messed up paths? or M2 causing issues

122 views Asked by At

I'm trying to run a code that requires to use pdfKit and wkhtmltopdf. But I keep getting errors. I suspect the code errors have something to do with the wkhtmltopdf installation. Although I have uninstalled and reinstalled it different ways to fix it. When I check for the wkhtmltopdf it says the latest version is already installed but when I check to see the version, it shows command not found. Here is some of the snippit of the terminal prompts:

`brew install Caskroom/cask/wkhtmltopdf

Warning: Not upgrading wkhtmltopdf, the latest version is already installed
shagufta@0587663393 ~ % wkhtmltopdf --version

zsh: command not found: wkhtmltopdf
shagufta@0587663393 ~ % brew --prefix wkhtmltopdf

Error: No available formula with the name "wkhtmltopdf"`

[I'm using a macOS M2 system]

#Import all necessary Libraries
import jinja2 #Web template engine - helps build expressive and extensible templates with placeholders to serve dynamic data. 
import csv #To read CSV files
import os #To interact with underlying operating system
import pdfkit #To create PDF files from HTML


getinput = input("Enter the source filename (without the csv extension): ") #Allows users to enter filename
filename = getinput + '.csv' #Adding extension to the filename

with open(filename, "rt", encoding='utf-8', errors='ignore') as f: #Open filename, read as text file and ignore any encoding errrors. variable 'f' = file
    reader = csv.reader(f) 
    headers = next(reader) 
    columns = {h: [] for h in headers} 
    for row in reader: 
        for h, v in zip(headers, row): 
            columns[h].append(v) 

# Initialize the report text
concat = "" 

# Iterate through all rows of data in CSV 
for i in range(len(columns["FormalName"])): 
    FormalNameZ = columns["FormalName"][i] 
    print(str(i) + columns["FormalName"][i]) 

    # If this is the first instance of the subject
    concat = ""
    templateLoader = jinja2.FileSystemLoader(searchpath="templates") 
    templateEnv = jinja2.Environment(loader=templateLoader) 
    TEMPLATE_FILE = "profile_1.html" 
    template = templateEnv.get_template(TEMPLATE_FILE) 
    
    #TemplateVars is a disctionary created, maps each header to its corresponding value for the current row. This dictionary will be used to populate the placeholders in the html template
    templateVars = {header: columns[header][i] for header in headers}
    
    #Template is rendered with the value from templateVars
    outputText = template.render(templateVars)
    concat = concat + outputText #output text from template is concatenated with existing content in concat
    
    concat = concat + "</table></body></html>" 
    
    outputPDF = 'C:/Users/shagufta/Desktop/ProjectSandbox/ProfileBook Mywork/templates/' + columns["FormalName"][i] + ".pdf" 
    path_wkhtmltopdf = '/usr/local/bin/wkhtmltopdf' 
    config = pdfkit.configuration(wkhtmltopdf=path_wkhtmltopdf) 
    
    pdfkit.from_string(concat, outputPDF, configuration=config) 
 

and the error I keep getting:

Traceback (most recent call last): File "/Users/shagufta/Desktop/ProjectSandbox/ProfileBook Mywork/profile_1.py", line 51, in pdfkit.from_string(concat, outputPDF, configuration=config) #Convert the concatenated html(concat) into a pdf file using the output filename and configuration File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pdfkit/api.py", line 75, in from_string return r.to_pdf(output_path) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pdfkit/pdfkit.py", line 201, in to_pdf self.handle_error(exit_code, stderr) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pdfkit/pdfkit.py", line 158, in handle_error raise IOError("wkhtmltopdf exited with non-zero code {0}. error:\n{1}".format(exit_code, error_msg)) OSError: wkhtmltopdf exited with non-zero code 1. error: QPainter::begin(): Returned false Exit with code 1, due to unknown error.

0

There are 0 answers