Package: pdf2image
pdf2imageA python (3.7+) module that wraps pdftoppm and pdftocairo to convert PDF to a PIL Image objectHow to installpip install pdf2imageWindowsWindows users will have to build or download poppler for Windows. I recommend@oschwartz10612 versionwhich is the most up-to-date. You will then have to add thebin/folder toPATHor usepoppler_path = r"C:\path\to\poppler-xx\bin" as an argumentinconvert_from_path.MacMac users will have to installpoppler.Installing usingBrew:brew install popplerLinuxMost distros ship withpdftoppmandpdftocairo. If they are not installed, refer to your package manager to installpoppler-utilsPlatform-independant (Usingconda)Install poppler:conda install -c conda-forge popplerInstall pdf2image:pip install pdf2imageHow does it work?frompdf2imageimportconvert_from_path,convert_from_bytesfrompdf2image.exceptionsimport(PDFInfoNotInstalledError,PDFPageCountError,PDFSyntaxError)Then simply do:images=convert_from_path('/home/belval/example.pdf')ORimages=convert_from_bytes(open('/home/belval/example.pdf','rb').read())OR better yetimporttempfilewithtempfile.TemporaryDirectory()aspath:images_from_path=convert_from_path('/home/belval/example.pdf',output_folder=path)# Do something hereimageswill be a list of PIL Image representing each page of the PDF document.Here are the definitions:convert_from_path(pdf_path, dpi=200, output_folder=None, first_page=None, last_page=None, fmt='ppm', jpegopt=None, thread_count=1, userpw=None, use_cropbox=False, strict=False, transparent=False, single_file=False, output_file=str(uuid.uuid4()), poppler_path=None, grayscale=False, size=None, paths_only=False, use_pdftocairo=False, timeout=600, hide_attributes=False)convert_from_bytes(pdf_file, dpi=200, output_folder=None, first_page=None, last_page=None, fmt='ppm', jpegopt=None, thread_count=1, userpw=None, use_cropbox=False, strict=False, transparent=False, single_file=False, output_file=str(uuid.uuid4()), poppler_path=None, grayscale=False, size=None, paths_only=False, use_pdftocairo=False, timeout=600, hide_attributes=False)What's new?Allow users to hide attributes when using pdftoppm withhide_attributes(Thank you @StaticRocket)Fix console opening on Windows (Thank you @OhMyAgnes!)Addtimeoutparameter which raisesPDFPopplerTimeoutErrorafter the given number of seconds.Adduse_pdftocairoparameter which forcespdf2imageto usepdftocairo. Should improve performance.Fixed a bug where usingpdf2imagewith multiple threads (but not multiple processes) would cause and exceptionjpegoptparameter allows for tuning of the output JPEG when usingfmt="jpeg"(-jpegoptin pdftoppm CLI) (Thank you @abieler)pdfinfo_from_pathandpdfinfo_from_byteswhich expose the output of the pdfinfo CLIpaths_onlyparameter will return image paths instead of Image objects, to prevent OOM when converting a big PDFsizeparameter allows you to define the shape of the resulting images (-scale-toin pdftoppm CLI)size=400will fit the image to a 400x400 box, preserving aspect ratiosize=(400, None)will make the image 400 pixels wide, preserving aspect ratiosize=(500, 500)will resize the image to 500x500 pixels, not preserving aspect ratiograyscaleparameter allows you to convert images to grayscale (-grayin pdftoppm CLI)single_fileparameter allows you to convert the first PDF page only, without adding digits at the end of theoutput_fileAllow the user to specify poppler's installation path withpoppler_pathPerformance tipsUsing an output folder is significantly faster if you are using an SSD. Otherwise i/o usually becomes the bottleneck.Using multiple threads can give you some gains but avoid more than 4 as this will cause i/o bottleneck (even on my NVMe SSD!).If i/o is your bottleneck, using the JPEG format can lead to significant gains.PNG format is pretty slow, this is because of the compression.If you want to know the best settings (most settings will be fine anyway) you can clone the project and runpython tests.pyto get timings.Limitations / known issuesA relatively big PDF will use up all your memory and cause the process to be killed (unless you use an output folder)Sometimes fail read pdf signed using DocuSign,Solution for DocuSign issue.