1

I'm trying to create a kind of page break function for pdfs using pikepdf.

Given some vertical position (dotted grey line in the image) I want to split the page into two pages. I want to keep the page size and move the elements under the line to the top of the next page.

enter image description here

I basically need to do 3 things:

  • List elements on page
  • Find the position of given elements
  • Move elements around the page

I'm struggling to find the relevant documentation on the pikepdf docs page. Could someone point me in the right direction?

iHnR
  • 125
  • 7
  • @KJ I figured so much. My idea was to just copy the page (fairly trivial with a list insertion), remove different parts and move elements. Your idea of moving the page itself down rather than the content up seems like a better solution, now that I've familiarized myself with some of the details on pdf. Is there another library better suited for this kind of job? – iHnR Oct 23 '22 at 11:17

1 Answers1

1

You should be able to do this in cpdf (or its Python library version pycpdflib). From the command line, you might do (untested):

cpdf in.pdf 1-3,3,4-end -o out.pdf

(duplicate page three as new page 4)

cpdf -trimbox "x y w h" out.pdf 3 AND -trimbox "x2 y2 w2 h2" -range 4 -o out2.pdf

(crop the duplicate pages for suitable x y w h values)

cpdf -hard-box /TrimBox out2.pdf 3,4 -o out3.pdf

(actually trim the page content to the new trim boxes, baking it in)

cpdf -remove-trim out3.pdf 3,4 -o out4.pdf

(remove the trim box, restoring the original page dimensions but leaving the hard box in place)

cpdf -shift "dx dy" out4.pdf 4 -o out5.pdf

(shift the lower part up to the top of the page for some dx dy)

johnwhitington
  • 2,308
  • 1
  • 16
  • 18
  • This worked perfectly. One small mistake is that you typed `-remove-trim`, which should have been `-remove-trimbox`. I haven't looked at how to integrate in python yet, but at least I have the desired result. – iHnR Oct 23 '22 at 15:28