skip to Main Content

I tried to follow a tutorial and programmed something via Python to convert an HTML file to PDF.
In my case "sample.html".

import pdfkit

#Define path to wkhtmltopdf.exe
path_to_wkhtmltopdf = r'C:Program Fileswkhtmltopdfbinwkthmltopdf.exe'

#Define path to HTML file
path_to_file = 'sample.html'

#Point pdfkit configuration to wkhtml.exe
config = pdfkit.configuration(wkhtmltopdf = path_to_wkhtmltopdf)

#Convert HTML file to PDF
pdfkit.from_file(path_to_file, output_path = 'sample.pdf', configuration = config)

But now I have a folder with many HTML files and it would be too much work to do it for each file individually. What part of the code would I need to change to achieve this?

2

Answers


  1. I add sample code below.

    import os
    import pdfkit
    
    path_to_wkhtmltopdf = r'C:Program Fileswkhtmltopdfbinwkhtmltopdf.exe'
    html_folder_path = r'path/to/html/files'
    config = pdfkit.configuration(wkhtmltopdf=path_to_wkhtmltopdf)
    html_files = [file for file in os.listdir(html_folder_path) if file.endswith('.html')]
    
    for html_file in html_files:
        input_html_path = os.path.join(html_folder_path, html_file)
        output_pdf_path = os.path.join(html_folder_path, f'{os.path.splitext(html_file)[0]}.pdf')
    
        pdfkit.from_file(input_html_path, output_path=output_pdf_path, configuration=config)
    
    print("Conversion completed.")
    
    Login or Signup to reply.
  2. You dont need Python or PDFkit to run WkHTMLtoX, but they do provide a wrapper to files and settings.
    However if you know the switches to get the desired layout, you can use them in a batch file, running from a working folder.

    Here are two ways of converting HTML to PDF (there are many others) and each has their merits. I include the batch file for you to place in a test set folder (and or sub folders) you will need to edit the exe folder names and add any switches desired.

    enter image description here

    htm2pdf.cmd

    @echo off
    SETLOCAL ENABLEDELAYEDEXPANSION
    
    REM Define path to wkhtmltopdf.exe
    set "wkhtm2x=C:Program Fileswkhtmltopdfbinwkhtmltopdf.exe"
    
    REM Define path to MSEdge.exe this should not need changing here
    set "msedge=%ProgramFiles%MicrosoftEdgeApplicationmsedge.exe"
    
    REM switch to working folder
    cd /d "%~dp0"
    
    FOR /F "usebackq tokens=* delims=" %%F in (`dir /s /b "*.htm*"`) do (
    
    echo running wkhtmltox
    "%wkhtm2x%" "%%F" "%%~dpnF-wkhm2.pdf"
    
    echo running edge htm2pdf
    "%msedge%" --headless --print-to-pdf="%%~dpnF-edge2.pdf" "%%F"
     )
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search