Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any script to convert folder images into one pdf

I have many folders and inside that i have many images. Now i want one PDF per folder so that all images contained in folder goes into PDF. I have 1000s of folders so i want something which can batchprocess or which can walk in the folder and start processing things.

like image 560
tej.tan Avatar asked Jun 21 '11 12:06

tej.tan


People also ask

How do I convert an entire folder to PDF?

Right-click the folder to show the context menu. Click 'Combine to one PDF' menu option. And choose 'Convert and combine all files into one continuous PDF file' option and click 'Continue'. From the 'Save As' dialog box, choose a folder path and name of the PDF file to be created.

How do I convert multiple files to PDF?

Convert multiple files into a single PDF. Open your favorite web browser and navigate to Acrobat. Select Combine Files. Drag and drop your files into the conversion frame. You can also locate your files manually.


2 Answers

I'd solve this with ImageMagick, and not with Python. ImageMagick has the console tool 'convert'. Use it like this:

convert *.jpg foo.pdf

See here. (Depends on whether you use Windows, Mac or Linux, should be easy to find out with Google)

like image 131
naeg Avatar answered Nov 15 '22 15:11

naeg


I used this code to do the same thing. It uses the Python (2.7 not Python 3)and the reportlab package downloadable from here http://www.reportlab.com/software/installation/ and loops thru all the subdirectories of what you set "root" to and creates one pdf of all the jpegs in each folder.

import os
from reportlab.pdfgen import canvas
from reportlab.lib.utils import ImageReader


root = "C:\\Users\\Harry\\" 

try:
   n = 0
   for dirpath, dirnames, filenames in os.walk(root):
       PdfOutputFileName = os.path.basename(dirpath) + ".pdf" 
      c = canvas.Canvas(PdfOutputFileName)
      if n > 0 :
           for filename in filenames:
                LowerCaseFileName = filename.lower()
                if LowerCaseFileName.endswith(".jpg"):
                     print(filename)
                     filepath    = os.path.join(dirpath, filename)
                     print(filepath)
                     im          = ImageReader(filepath)
                     imagesize   = im.getSize()
                     c.setPageSize(imagesize)
                     c.drawImage(filepath,0,0)
                     c.showPage()
                     c.save()
      n = n + 1
      print "PDF of Image directory created" + PdfOutputFileName

except:
     print "Failed creating PDF"
like image 28
Harry Spier Avatar answered Nov 15 '22 14:11

Harry Spier