Re: Automating PDF Export in LibreCAD
Posted by cenaluc on Aug 02, 2024; 1:49pm
URL: https://forum.librecad.org/Automating-PDF-Export-in-LibreCAD-tp5725113p5725187.html
Hi,
Thanks
I could not get proper output quality with dxf2pdf but I found a nice way to do it with pyautogui
See here:
import time
import os
import pyautogui
def open_data(app_instance):
try:
# Open the Excel file with the default application
os.startfile(app_instance.jobfilexls)
except Exception as e:
print(f"An error occurred: {e}")
# def on_button_hover(app_instance, event):
# app_instance.write_button["background"] = "#A9A9A9"
#
# def on_button_leave(app_instance, event):
# app_instance.write_button["background"] = "#D3D3D3"
def open_cad(app_instance):
print('ok')
try:
# Open the Excel file with the default application
os.startfile(app_instance.jobfiledxf)
except Exception as e:
print(f"An error occurred: {e}")
def print_pdf(app_instance):
# Get the folder and filename without extension
folder_path = os.path.dirname(app_instance.jobfiledxf)
file_name = os.path.basename(app_instance.jobfiledxf).replace('.dxf', '')
# Define the path for the PDF
pdf_path = os.path.join(folder_path, f"{file_name}.pdf")
# Open LibreCAD
print("Opening LibreCAD...")
os.startfile("C:\\Program Files (x86)\\LibreCAD\\LibreCAD.exe")
time.sleep(5) # Wait for LibreCAD to open
# Handle input device dialog
print("Handling input device dialog...")
time.sleep(2) # Give some time for the dialog to appear
pyautogui.press('enter')
time.sleep(2) # Wait for the dialog to close
# Open the DXF file
print("Opening DXF file...")
pyautogui.hotkey('ctrl', 'o')
time.sleep(2)
pyautogui.typewrite(app_instance.jobfiledxf)
pyautogui.press('enter')
time.sleep(5) # Wait for the file to open
# Navigate to Print Preview
print("Navigating to Print Preview...")
pyautogui.hotkey('alt', 'f') # Open file menu
time.sleep(1)
pyautogui.press('down', presses=9, interval=0.2) # Navigate to 'Print Preview'
pyautogui.press('enter')
time.sleep(2)
# Export as PDF
print("Navigating to Export as PDF...")
pyautogui.hotkey('alt', 'f') # Open file menu
time.sleep(1)
pyautogui.press('down', presses=7, interval=0.2) # Navigate to 'Export'
pyautogui.press('right')
time.sleep(1)
pyautogui.press('down', presses=1, interval=0.2) # Navigate to 'Export as PDF'
pyautogui.press('enter')
time.sleep(2)
# Save PDF and handle overwrite prompt
print(f"Saving PDF as {pdf_path}...")
pyautogui.typewrite(pdf_path)
pyautogui.press('enter')
time.sleep(2) # Wait for the "Save As" dialog to respond
# Handle the overwrite prompt if it appears
# Most dialogs will accept 'Enter' to overwrite, but this depends on LibreCAD's behavior
pyautogui.press('enter')
time.sleep(2) # Wait for the save operation to complete
# Navigate to Quit
print("Navigating to Quit...")
pyautogui.hotkey('alt', 'f') # Open file menu
time.sleep(1)
pyautogui.press('down', presses=12, interval=0.2) # Navigate to 'Print Preview'
pyautogui.press('enter')
pyautogui.press('right')
time.sleep(1)
pyautogui.press('enter')
time.sleep(2)
print("LibreCAD closed.")