6

I want to write a python script to convert PNG's into 2-page pdfs (i.e. 2 PNGs per PDF). The software needs to run on both a Mac and Windows 7.

My current solution is using ReportLab, but that doesn't install easily on a Mac. According to its website, it only has a compiled version for Windows. It has a cross-platform version that requires a C compiler to install.

Is there a better way to do this (so that I don't have to install a C compiler on the Mac)? Should I use a different library, or a different language entirely? As long as I can call the program from a python script, I could use any language for the pdf creation. Or, is there a very straightforward (i.e a non-programmer could install it) C compiler that I could install on a Mac?

What do you recommend?

5 Answers 5

4

The unix program convert can help you for conversion.

convert file.png file.pdf

But you said you want to have it under windows too. PIL imaging library has a PDF module. you should try a simple

pilconvert file.png file.pdf

to put more than one image you can play with the library which is quite flexible for resizing, stitching, etc. It is also available for mac and windows

Update 2011-02-15

On my macosx, I have installed reportlab without difficulties.

% sudo easy_install reportlab
Searching for reportlab
Reading http://pypi.python.org/simple/reportlab/
Reading http://www.reportlab.com/
Best match: reportlab 2.5
Downloading http://pypi.python.org/packages/source/r/reportlab/reportlab-2.5.tar.gz#md5=cdf8b87a6cf1501de1b0a8d341a217d3
Processing reportlab-2.5.tar.gz

So you could use a combination of PIL and Reportlab for your own needs.

Sign up to request clarification or add additional context in comments.

1 Comment

Does PIL have functionality to make multipage PDF's? I want the PDF's to be 2 pages long.
1

Reportlab is one of the good tools for generating pdfs. If you can use it for your purposes, it is better to stick with it. For installation on MAC, I see that darwinports have a port for Reportlab called py-reportlab. Follow the instructions to install it using portage, it will install the dependencies by itself.

Comments

1

PyCairo could be a good alternative. You'll have full control and less dependencies, but reportlab is a lot simplier.

Comments

1

Why anyone hasn't tried this??

If you are not much concern about the quality, try the following solution.

import PIL.Image

filepath = "temp.png"
newfilename = 'our.pdf'


im = PIL.Image.open(filepath)

im.save(newfilename, "PDF", quality=100)

2 Comments

Wow, you weren't kidding about the quality -- it's terrible. Is PIL the only way to convert PNG to PDF?
No it is not the only way but the quality that PIL is giving, is ok for me and it is not like you are saying, quality is not worse. Quality is ok and PIL is very easy to implement.
0
from reportlab.lib.pagesizes import letter
from reportlab.lib.utils import ImageReader
from reportlab.pdfgen import canvas

def convert_png_to_pdf(png_folder, output_pdf_path):
    c = canvas.Canvas(output_pdf_path, pagesize=letter)

    # Get a list of all PNG files in the folder
    png_files = [f for f in os.listdir(png_folder) if f.endswith('.png')]

    for png_file in png_files:
        png_path = os.path.join(png_folder, png_file)

        # Open the PNG image using Pillow
        image = ImageReader(png_path)

        # Add the image to the PDF canvas
        c.setPageSize((image.getSize()[0], image.getSize()[1]))
        c.drawImage(image, 0, 0)

        # Create a new page
        c.showPage()

    # Save the resulting PDF file
    c.save()

    print(f'PDF file created successfully: {output_pdf_path}')

# Provide the folder path containing the PNG files
png_folder_path = r"C:\Insert\File\Path\Here"

# Provide the output PDF file path
output_pdf_path = r"C:\Output\Path\Location\Here\file.pdf"

# Call the function to convert PNG to PDF
convert_png_to_pdf(png_folder_path, output_pdf_path)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.