-1

I need to take an existing PDF (created with Prawn), and combine pairs after page 1 (the cover) into single pages. I would also like to add a vertical line in the center of the joined pages. The pages are to be printed in books, and the goal is to make single PDF pages that are similar to the side by side view in Acrobat. I know I can convert them to images, do what I need to with ImageMagick, then put them back into a PDF format, but I am trying to minimize the number of conversions so I can save as much quality as possible.

I also realize I can do this from the start with Prawn, but I am trying to avoid that as it would require a very large change to our application.

Brandon
  • 1,735
  • 2
  • 22
  • 37
  • Isn’t this something better delegated to the “print” dialogue box in the OS? – Myst Apr 26 '18 at 05:40
  • Imagemagick will rasterize your PDF files when reading. You can append them side by side in pairs with a script. But when you write them back to PDF, you will get the raster files inbedded in a PDF vector shell. The will be much larger than your original PDF pages. – fmw42 Apr 26 '18 at 06:04
  • @Myst this has to be done in a background job of a Ruby on Rails application. Thanks for the idea though. – Brandon Apr 26 '18 at 14:49
  • @fmw42 That is a good point, thanks. Another reason I am trying not to do that is to avoid any quality loss. – Brandon Apr 26 '18 at 14:50
  • I think you can do this with CombinePDF, but I'm a bit in a rush. I'll try to outline a solution later. – Myst Apr 26 '18 at 15:18
  • I was looking at that as a possibility but didn't see an option. I'm happy to read anything you come up with! – Brandon Apr 26 '18 at 21:42

1 Answers1

1

It is possible to do this with Ghostscript and the pdfwrite device, but its by no means simple. You need to write some PostScript to do the job.

You would need to add BeginPage and EndPage procedures, the BeginPage would need to check the current page number (and you would need to track this yourself). If its page 1, process normally. If its an even page, throw away the current PageSize and replace it with one which covers a pair of pages. Process the even page. Do not transmit the content.

If the page is odd (and not 1) then translate the origin so that its offset to the right by the width of the page. Process the odd page. use moveto, lineto and stroke to draw the required line between the two pages. Transmit the page.

This assumes that all the pages are the same size and orientation, or least that the sizes of each page are known in advance. It would be possible to retrieve those programmatically as well, but more complex.

Its definitely non-trivial, but if you rummage through my answers in the PostScript tags and look for anything with the word 'imposition' you'll probably find program outlines to do the job.

I did a quick look and here's an answer I wrote some time back. It uses a different approach to that outlined above, it copies some of the guts of the PDF interpreter and repurposes them. It does a chunk of what you want though.

KenS
  • 30,202
  • 3
  • 34
  • 51
  • I will take a look at this. Thank you for the guidance! The size of all pages after the first are the same, and at the moment they are always the same size, but that will not always be true. Because of that, it will be better to grab the size programmatically. Thanks again. – Brandon Apr 26 '18 at 14:53