0

what I'm trying to do is to crop and resize a large svg/pdf (easy example would be size A3) to A4 for poster print with overlapping. The dimensions of the A3 are 420mm x 297mm. The script should cut out a section of 180mm x 257mm (which is A4 minus 20mm border on each side) and paste it centered into an A4 page or save the cropped section first and then add borders to the cropped file to result in the A4 dimension. If I get this part to work I can do a loop to move the cropped area according to the needed overlapping

I found some hints here:

But nothing that quite fit exactly what I need as it seems that cropping svg/pdf is by default non-destructive. I really want to get rid of the information outside of my cropping area that when resizing it doesn't show up anymore.

I searched a lot but couldn't quite find a fitting solution. I'm not lazy to write and test code, but with 2 small children and a full time job my time is very limited. Therefore I would really appreciate if someone could help my out with a very basic code snippet or pointing me to the right direction that I'm not wasting time trying things out that lead to a dead end :)

Thanks in advance for the help, it's really appreciated :)

Cheers, Toby

canedha
  • 53
  • 8
  • getting crazy here. it seems that there is no way to simply crop an pdf/svg and deleting anything outside media,bleed,viewbox... if i try rendering with e.g. cairo i get errors as the full page is very large :( anyone any idea? – canedha May 27 '20 at 13:59

1 Answers1

1

The second link you found contains useful code. What is missing there is that the crop region of a page only instructs the reader to hide everything outside the cropped region. Because the content is still there, as evident in the linked question.

So to overcome this, proceed like in the linked question but when it comes to using the contents of the page, make sure that you also clip to the cropped region. Ie. save the graphics state, define a clipping path equivalent to the cropped region, then invoke the page.mergeScaledTranslatedPage, then restore the graphics state.

gettalong
  • 735
  • 3
  • 10
  • thanks for the help! i tried it but unfortunately i can't get the code to run and do not know exactly where to insert the mergePage. also the stream usage of pypdf is still confusing for me. could you please give me a short sample? – canedha May 24 '20 at 06:11
  • I'm sorry but I'm not familiar with pypdf or Python in general to provide detailed instructions. – gettalong May 25 '20 at 11:20