└── converter.py /converter.py: -------------------------------------------------------------------------------- 1 | import os 2 | import pdfkit 3 | import tkinter as tk 4 | from tkinter import filedialog 5 | 6 | # Establecer la ruta de wkhtmltopdf directamente en el entorno de Python 7 | wkhtmltopdf_path = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe' 8 | os.environ['PATH'] += os.pathsep + os.path.dirname(wkhtmltopdf_path) 9 | 10 | class HTMLtoPDFConverterApp: 11 | def __init__(self, root): 12 | self.root = root 13 | self.root.title("HTML to PDF Converter") 14 | 15 | # Configura el botón para seleccionar el archivo HTML 16 | self.select_html_button = tk.Button(root, text="Seleccionar HTML", command=self.select_html_file) 17 | self.select_html_button.pack(pady=10) 18 | 19 | # Configura el botón para convertir a PDF 20 | self.convert_button = tk.Button(root, text="Convertir a PDF", command=self.convert_to_pdf) 21 | self.convert_button.pack(pady=10) 22 | 23 | def select_html_file(self): 24 | file_path = filedialog.askopenfilename(filetypes=[("Archivos HTML", "*.html")]) 25 | if file_path: 26 | self.html_file_path = file_path 27 | print(f'Se ha seleccionado el archivo HTML: {self.html_file_path}') 28 | 29 | def convert_to_pdf(self): 30 | if hasattr(self, 'html_file_path'): 31 | pdf_file_path = filedialog.asksaveasfilename(defaultextension=".pdf", filetypes=[("Archivos PDF", "*.pdf")]) 32 | 33 | if pdf_file_path: 34 | try: 35 | # Configura las opciones de PDF 36 | options = { 37 | 'quiet': '', 38 | 'no-images': '', 39 | } 40 | 41 | # Convierte el archivo HTML a PDF 42 | pdfkit.from_file(self.html_file_path, pdf_file_path, options=options) 43 | 44 | print(f'Se ha creado el archivo PDF: {pdf_file_path}') 45 | 46 | except Exception as e: 47 | print(f"Ha ocurrido un error: {str(e)}") 48 | else: 49 | print("Selecciona un archivo HTML antes de convertir a PDF.") 50 | 51 | if __name__ == "__main__": 52 | root = tk.Tk() 53 | app = HTMLtoPDFConverterApp(root) 54 | root.mainloop() 55 | --------------------------------------------------------------------------------