2

I have pdf which have same paper size (A6) and multiple pages. What i want to do is to print 2 pages into one sheet of paper (A4) using python. Try to automate thing here which is why i use Python.

I know i can print stuff using GSPrint with Python but i never quite get how to do this with it.

import win32print
import win32api
import autoit
import os

dir_print = r'C:\Users\anom\Google Drive\Programming\test\New folder'
list_tracking = os.listdir(dir_print)

for name in list_tracking:
    print('Printing - ', name)
    GHOSTSCRIPT_PATH = r"C:\Users\anom\Google Drive\Programming\test\GHOSTSCRIPT\bin\gswin32.exe"
    GSPRINT_PATH = r"C:\Users\anom\Google Drive\Programming\test\GSPRINT\gsprint.exe"
    filename = os.path.join(dir_print, name)
    win32api.ShellExecute(0, 'open', GSPRINT_PATH, '-ghostscript "'+GHOSTSCRIPT_PATH+'" -printer "RICOH SP 150SU" -color -dPDFFitPage "'+filename+'"', '.', 0)

Its got me thinking to resize the page to A4 first then print which is doable with Pypdf2 module (maybe...) But i stuck at printing 2 pages into one sheet of paper.

1 Answers1

0

Here you go... for both A5 (two per page) and A6 (4 per page) You'll need to pip install Pillow

import fitz
from fpdf import FPDF
from PIL import Image
from io import BytesIO

def pixmap_to_image(pixmap):
    img_data = pixmap.samples
    img = Image.frombytes("RGB", [pixmap.width, pixmap.height], img_data)
    return img

input_pdf_path = "C:\\input folder path\\input file.pdf"
output_4_per_page = "C:\\input folder path\\output file A6.pdf"
output_2_per_page_landscape = "C:\\input folder path\\output file A5.pdf"

# Read the original PDF
doc = fitz.open(input_pdf_path)

# Create 4 sheets per page
pdf_4_per_page = FPDF()
for i in range(0, len(doc), 4):
    pdf_4_per_page.add_page()
    for j in range(4):
        page_idx = i + j
        if page_idx < len(doc):
            pixmap = doc.load_page(page_idx).get_pixmap()
            img = pixmap_to_image(pixmap)
            buffer = BytesIO()
            img.save(buffer, format="PNG")
            buffer.seek(0)
            x_offset = (j % 2) * 105
            y_offset = (j // 2) * 148.5
            pdf_4_per_page.image(buffer, x=x_offset, y=y_offset, w=105, h=148.5)
pdf_4_per_page.output(output_4_per_page)

# Create 2 sheets per page in landscape
pdf_2_per_page_landscape = FPDF(orientation="L", unit="mm", format="A4")  # A4 landscape
for i in range(0, len(doc), 2):
    pdf_2_per_page_landscape.add_page()
    for j in range(2):
        page_idx = i + j
        if page_idx < len(doc):
            pixmap = doc.load_page(page_idx).get_pixmap()
            img = pixmap_to_image(pixmap)
            buffer = BytesIO()
            img.save(buffer, format="PNG")
            buffer.seek(0)
            x_offset = j * 148.5  # Half of A4 width in mm
            y_offset = 0
            pdf_2_per_page_landscape.image(buffer, x=x_offset, y=y_offset, w=148.5, h=210)  # A5 size in landscape
pdf_2_per_page_landscape.output(output_2_per_page_landscape)
Stu
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 26 '23 at 18:44