Skip to content

Instantly share code, notes, and snippets.

@hellpanderrr
Created March 30, 2020 19:52
Show Gist options
  • Save hellpanderrr/ab3aeb4709d5eaab93b0ffdd435330a2 to your computer and use it in GitHub Desktop.
Save hellpanderrr/ab3aeb4709d5eaab93b0ffdd435330a2 to your computer and use it in GitHub Desktop.
Convert a list of svg files into one pdf document
from PyPDF2 import PdfFileMerger
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPDF
def svgs_to_pdf(file_paths=[r'C:\1.svg',r'C:\2.svg',r'C:\3.svg'], output_folder='tmp', pdf_name='test.pdf'):
"""
Convert svg files into pdfs, save pdfs in output_folder, merge them in one pdf file.
@param file_paths: full paths to svg files
@param output_folder: path to folder for saving temporary pdfs
@param pdf_name: name for resulting pdf file.
"""
if not os.path.isdir(output_folder):
os.mkdir(output_folder)
for n,f in enumerate(file_paths):
drawing = svg2rlg(f)
renderPDF.drawToFile(drawing, os.path.join('tmp', '%s.pdf' % n))
pdfs = glob.glob(os.path.join(output_folder,'*.pdf'))
pdfs.sort(key=os.path.getmtime)
merger = PdfFileMerger()
for pdf in pdfs:
merger.append(pdf)
merger.write(pdf_name)
merger.close()
for pdf in pdfs:
os.remove(pdf)
@naweka
Copy link

naweka commented Dec 17, 2024

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment