def pdf_to_png(pdf_path, poppler_path, dpi=300):
images = convert_from_path(pdf_path, dpi=dpi, poppler_path=poppler_path)
image_paths = []
for i, image in enumerate(images):
image_path = f"page_{i+1}.png"
image.save(image_path, "PNG")
image_paths.append(image_path)
return image_paths
def png_to_pdf(image_paths, output_pdf, dpi=300):
pdf = None
for image_path in image_paths:
image = Image.open(image_path)
width, height = image.size
width_pt = width * 72 / dpi
height_pt = height * 72 / dpi
if pdf is None:
pdf = FPDF(unit="pt", format=[width_pt, height_pt])
pdf.add_page()
pdf.image(image_path, 0, 0, width_pt, height_pt)
pdf.output(output_pdf, "F")
def convert_pdf_to_pdf(pdf_path, output_pdf, poppler_path, dpi=300):
image_paths = pdf_to_png(pdf_path, poppler_path, dpi)
png_to_pdf(image_paths, output_pdf, dpi)
pdf_path = "form.pdf" # Path to the input PDF
output_pdf = "output.pdf" # Path to the final PDF
poppler_path = r"C:\\poppler-24.08.0\\Library\\bin"
convert_pdf_to_pdf(pdf_path, output_pdf, poppler_path)
print("Conversion complete!")