How can I generate a PDF with two or more pages with reportlab? I've been unable to find anything in the documentation.
canvas.showPage()
will force a new page (even though it sounds like its showing a page,)
(assuming you are using the canvas)
if you are using flowables I think there is a PageBreak flowable
I think you can just keep .append()ing stuff and it will break the pages automatically once it get's too large for a single page, or you can force a page break by doing:
.append(PageBreak())
Related
I'm using the docx package in Python and I have many small tables which I want to put into the docx Word file. However, there are times when a table may cross the page (for example, half the table will be on Page 1 and the other half will be on Page 2). Is there a way to stop this from happening such that the full table will be printed on only one page?
I tried doing things like table.autofit = False and table.allow_autofit = False but I don't think they're what I need, so if anyone can help me out with it I would greatly appreciate it, thanks!
There is a pagination property called "keep_together". This is its description in the documentation:
keep_together causes the entire paragraph to appear on the same page, issuing a page break before the paragraph if it would otherwise be broken across two pages.
I have not tried this in code, but it is how I would format the tables if I was doing it in Word.
This is a link to the documentation:
https://python-docx.readthedocs.io/en/latest/user/text.html?highlight=break#pagination-properties
I have an FPDF2 table created using this script. I used to output it to a blank page and merge it to an existing pdf, which works fine.
But now we need to add the table to an existing page in the pdf and then if it doesn't fit, we insert new pages. And that's the problem.
FPDF doesn't seem to be able to draw to an existing page. I know I can use reportlab canvas can.drawString() to draw to an existing page, but I don't know if reportlab can draw an FPDF object.
Also, if I were to ditch FPDF and use only reportlab to draw a table, I don't know how to detect the end of the page and insert a new page if needed. I'm not starting at the start of a page, I'll be starting somewhere in the middle.
I would prefer to be able to use the FPDF2 script I already have and somehow add the output at a specific x,y position in a page though, if possible. Have you ever had this issue?
I also have Pypdf2 installed and used in the same project, but I think that only reportlab can do the job. Maybe I need to detect the end of the page via Pypdf2 and write to the page via reportlab?
Since no answer exists as of now and since I need an answer to finish my task, I did the following:
I added the FPDF table to a blank PDF page, and pushed it down, by using set_y(100), to have a half blank page to work with.
And I took a screenshot of the items which need to be placed above the table and then added them to the same page by using reportlab canvas.drawImage()
If there's a better solution, please post an answer and I'll accept it and refactor my code. For now, I'll accept my answer to close this question.
I am building a fairly simple document in reportlab with platypus. It is basically a header on all the pages and then a table object with line items that will extend onto multiple pages.
What I am trying to figure out is if there is a way to specify one page template for the first page, and a page template for all following pages.
From what I can tell you have to put a call to NextPageTemplate as a flowable in your story, but since one flowable takes up multiple pages I can't put a NextPageTemplate call in there.
I thought there was a way to specify a template onFirstPage and a template onLaterPages when building the document but I can't seem to find that anymore.
Any Ideas?
Alright I figured it out. Hope this will help someone else in the future.
Where I had seen onFirstPage and onLaterPages was in the build method of the SimpleDocTemplate class. And while for a simpler report it would have worked fine, for mine that method does not work. I am using a frame to specify the margins of my document, there is probably a better way to do this, and the SimpleDocTemplate creates it's own margin Frame, also I might be wrong about that.
Anyways, I subclassed BaseDocTemplate to overide the handle_pageBegin method to tell the build method to switch to the second page template like so:
def handle_pageBegin(self):
'''override base method to add a change of page template after the firstpage.
'''
self._handle_pageBegin()
self._handle_nextPageTemplate('Later')
Then I could just add 2 page templates into the document when I create it naming the second one 'Later'.
Seems to work great for now.
I am using the python-docx module to make lots and lots of documents generated on demand from data. There are several parts when I insert figures with the conventional document.add_picture command like so:
document.add_picture(img_after_crop, width=Inches(width))
The problem I'm having is that many of the pictures are charts that finish a section so that after the chart I want to move to a new page, but the images are of different sizes so that sometimes they have already filled the page. Thus if I put a page break after the image I will sometimes get a nice figure then a new page of the next section and other times (if the image fills the page) the flow has already moved on to the next page and a page break will leave an empty page. I'm looking for a way to prevent this. If there is a good way to get current page number, I can see how to fix the problem. Would moving to using "sections" fix this (for example do sections automatically start a new page but not advance the page if we're already on a blank page)? Detecting page advancement in a document being worked on would be ideal, but I'm open to any solution...
SOLUTION
The correct answer in short form is below, but it took me some trial and error to figure out the right way to use it so for the record here is a minimum code snippet on how to use page_break_before to make a heading or paragraph jump to the next page:
from docx import Document
document = Document()
document.add_heading('Document title', 0)
para = document.add_heading("This heading goes at the top of a new page", level=1)
para.paragraph_format.page_break_before=True
document.add_paragraph('Just some text goes here. This is a normal paragraph')
document.save('Doctest.docx')
This python will generate a test word document where the title is on the first page then the heading jumps to the next page and the following paragraph just goes beneath it. The key trick is the paragraph_format access and that lets you also use alignment settings, keep_together settings, and so on from the paragraph_format options. I needed to cobble it together from a couple examples to figure out where exactly to set the value so I figured I'd post it for posterity.
Try setting the paragraph formatting of the paragraph immediately after the image to .page_break_before = True. The Word rendering engine is intelligent enough to not add an extra page break if the paragraph happens to occur first on a new page.
I am using rst2pdf to generate a PDF. I am using links to sections and they appear as hyperlinks in the PDF. If I hover over the link I can see it says "Go to page XXX". Is there a way to insert that page number into the text, so that it can be seen on hardcopies?
I'm starting using reportlab recently. Maybe you need to use the superscript tag?
p = Paragraph("<link href='http://someurl' color='blue'><u>Some text</u><super> [goto page xx]</super></link>", customstyle)
What it may looks like