I'm trying to automatically generate some pdf format reports in Python. I have figures that I want in the reports, but the figures are currently saved as pdfs. Saving the figures as something else is an option, but not ideal for what I'm trying to do. I've found examples (http://code.google.com/p/pdfrw/wiki/ExampleTools) using pdfrw and reportlab to turn pages from one pdf into pages of a new pdf, but I don't want them to be entire pages of my new pdf, just a figure occupying a section of the page. I haven't used pdfrw before, so I don't know a lot about the Canvas method and what it is fully capable of.
Asked
Active
Viewed 2,234 times
1
-
It's basically a three step process. Since PDFs can be multi-page, the first thing to be done to is extract just the page you want -- there appears to be some "subset" examples. The next thing to do would be to resize it to fit into the area where the figure is to appear -- again there's an example of resizing one. Lastly, it will have to be moved or translated to the position of that area on the final page -- there appears to be something like that going in the 4up example. – martineau Feb 18 '13 at 00:56
-
Unless I'm misunderstanding the 4up example, it looks like the translating and scaling is of the canvas, and then the call to `makerl(canvas, page)` replaces the entire canvas with the pdf. Admittedly, I don't quite understand the scaling of that canvas in that example – Kyle Heuton Feb 18 '13 at 03:36
-
1Yeah, transformations can be confusing because there's always two way to think about them -- from the outside in and vice-versa. The way I think about the PDF imaging model is that there's always a transformation matrix in effect that transforms the coordinates of what is being drawn into some kind of device coordinates, so a `canvas.translate(dx, dy)` call will cause `dx` & `dy` to be added to the x & y coords of everything that follows. Likewise a `canvas.scale(sx, sy)` call will scale subsequent graphics by the amounts `sx` and `sy`, then translate them by the `dx` & `dy`. _continued_ – martineau Feb 18 '13 at 11:06
-
1... In the 4up example the `makerl()` call converts each pdfrw input page into a reportlab object and returns it, then adds that to the canvas output object being created with a `canvas.doForm()` call with a global translate + scale transformation in effect -- one that makes it 1/4 normal size and moves it into the proper quadrant of the canvas. These translate and scaling operations are wrapped in `canvas.saveState()` and `canvas.restoreState()` calls so they will only apply the `canvas.doForm()` call which occurs between them for each quarter-sized input page processed. Hope this helps. – martineau Feb 18 '13 at 11:32
1 Answers
0
The trick to using a part of a page with the pagemerge
canvas is the ViewInfo
object. With it, you can describe, among other things, rectangle coordinates (e.g. when you are adding a page to a PageMerge
object).
ViewInfo
objects are defined and described in more detail in the buildxobj.py
file.
I should make another example and some documentation for that, but here is a similar stackoverflow question that I answered awhile back. HTH
(Disclaimer: I am the pdfrw author.)

Community
- 1
- 1

Patrick Maupin
- 8,024
- 2
- 23
- 42