0

I have a PDF with the art of a ticket for a fundraising dinner. I am providing a mock-up here so you can reproduce my problem:

mock up ticket

I would like to run the following pseudocode:

for i in 1:200
    copy "mock up.pdf" to $i.pdf
    inject $i into $i.pdf using font "OpenDyslexic" # place the ticket number in the pdf
end
create "final.pdf"
i = 0
for p in 1:20
    add page to "final.pdf"
    for column in 1:2
        for row in 1:5
            i = i + 1
            inject $i.pdf in "final.pdf" in row, column of page p
        end
    end
end

Thank you!

Iuri Gavronski
  • 513
  • 4
  • 7
  • I never used cpdf before. Does not seem to be free. The actual command would be ```cpdf -add-text -bottom-right 10 "$i" mockup.pdf -o $i.pdf``` – Iuri Gavronski Apr 06 '22 at 12:15
  • Unfortunately, I don't have too much time to research and test all those options. I need to print the tickets for my fundraising event this week. I exported my pdf file as png, imported it into an MS-Word file, and numbered them using mail merge. Not the best quality, but given my tech skills and time at hand, that's what I can do. Thanks for your suggestions, though. – Iuri Gavronski Apr 07 '22 at 21:56
  • @KJ, alternatively, do you believe I could use PIL in Python for that purpose? – Iuri Gavronski Apr 07 '22 at 22:19
  • such as https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html#fonts – Iuri Gavronski Apr 07 '22 at 22:25
  • And your script at https://stackoverflow.com/questions/69415164/pymupdf-how-to-convert-pdf-to-image-using-the-original-document-settings-for/69416971#69416971 – Iuri Gavronski Apr 07 '22 at 22:29
  • Well, @KJ, you are much more versed in such things than I do, considering the posts I mentioned. For now, I am done with MS Word, but I would love to have some sort of automation that speeds up the process of making such numbered monsters. My design tool is Canva.com, which outputs only the design in a decent resolution in PDF format, so I am stuck with the use case I just described. – Iuri Gavronski Apr 11 '22 at 14:23

1 Answers1

1

I might have a solution:


#!/bin/env python3
# adapted from https://pymupdf.readthedocs.io/en/latest/faq.html#how-to-insert-text
# and https://pymupdf.readthedocs.io/en/latest/faq.html#how-to-combine-single-pages
import fitz # in fact, pip install pymupdf
#from sys import argv # in a future, I might get all the parameter via command line
width, height = fitz.paper_size("A4")
r = fitz.Rect(0, 0, width, height)
doc = fitz.open("mock up.pdf")
page = doc[0]
print("input file information:")
print(doc.metadata)
print("rotation=",page.rotation)
print("cropbox=",page.cropbox)
print("mediabox=",page.mediabox)
print("rect=",page.rect)
page.set_rotation(0)
artsize=page.rect
(nx, ny) = (200,140) # position of the ticket number inside the pdf
(dx, dy) = page.mediabox_size # the displacement of the ticket inside the output pdf
ntickets=25
nrows=5 # of tickets vertically
ncols=2 # of tickets horizontally
ntickets_per_page = nrows*ncols
outpdf = fitz.open()

nrow = ncol = 0
i = 0

while i < ntickets:
    if i % ntickets_per_page == 0:
        #print("new page for ticket #",i)
        newpage = outpdf.new_page()
        nrow, ncol = 0, 0
    for ncol in range(1,ncols+1):
        for nrow in range(1,nrows+1):
            i += 1
            if i > ntickets:
                break
            text = "{:04d}".format(i)
            locr = fitz.Rect((ncol-1)*dx,(nrow-1)*dy,ncol*dx,nrow*dy)
            #print("location of the ticket:", locr)
            newpage.show_pdf_page(locr,doc,0)

            p = fitz.Point(nx+(ncol-1)*dx,ny+(nrow-1)*dy)
            #print("location of the number for ticket ", i, ": ", p)
            rc = newpage.insert_text(p, # bottom left  of 1st char
                    text,
                    fontname="tibo", # Times, bold
                    fontsize=12,
                    rotate=0,
                    )
i -= 1
print("%i lines printed on %i tickets." % (rc, i))
outpdf.save("tmp{:04d}.pdf".format(i))

Iuri Gavronski
  • 513
  • 4
  • 7