5

I want a text on each page of a pdf. This text is a html code that looks like <p style="color: #ff0000">blabla</p> as to appear red on the final doc, I convert it in pdf (html2pdf lib) then I merge it (PyPDF2 lib) to each page of my pdf. ...but the merging is very slow !

My question would be : Is there a faster way to merge pdf than page.mergePage method of PyPDF2 ? (Or maybe is there a faster way to add my text to this pdf?)

Thanks ! (using python 2.7.5 on Windows 8)

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
J'hack le lezard
  • 413
  • 7
  • 23
  • What are the constraints on the text you want to add? Do you need to use a specific font? Do you need to control where on the page the text shows up? – Felipe Jun 02 '15 at 18:21

1 Answers1

0

Since all you are doing is adding some text to the page, you can probably speed up the process by just editing the pages' content streams directly. Merging has to deal with fonts, other resources, crop boxes, etc. that slow down the process significantly. If you actually need to modify some of these things, the solution becomes more complex. Some example code:

TEXT_STREAM = []  # The PS operations describing the creation of your text
def add_text(page):
    "Add the required text to the page."
    contents = page.getContents()
    if contents is None:
        stream = ContentStream(TEXT_STREAM, page.pdf)
    else:
        contents.operations.extend(TEXT_STREAM)
Felipe
  • 3,003
  • 2
  • 26
  • 44