Forum Archive

Adding a page header to PDF output

NikkiSchwartzVB

Per usual, I've searched the forum and found ... nothing. 😂

Anyway to add a header to each page of the PDF output? Ideally this would include the client's name, dob, and the page number of the PDF itself... too much to ask? I'm crossing my fingers. (Both client's name and dob are stored in separate files from earlier workflows in the process)

I could do this as an outside process in PDFOffice, but automated in editorial would be waaaAaay better.

TIA!

I just wondered if I could somehow abuse the footnote for this purpose? I'd prefer it at the top starting on p2... but ya know, beggars can't be choosers,,, so I'd be happy with any kind of fix for this.

NikkiSchwartzVB

Adding page numbers and headers/footers seems to be elusive, after some more digging.

And I thought this would be the easy part.

@ccc any ideas?

ccc

@omz Would you consider adding header and footer parameters to Editorial action html-to-pdf?

Alternatively, is there any advise on how to edit a preexisting .pfd doc to add a header and/or footer?

NikkiSchwartzVB

The best idea I can come up with is to open the file in PDFExpert, where it is added to the client's file and then use a stamp to add their name and DOB to the top of each page. It's cumbersome, but in the last step I have editorial Add that information to the clipboard. So, at least I don't have to type it all in when I create the new stamp.

The only pdf app that seems to add page numbers is pdfpen. But it doesn't have customized stamps on ios... So it would mean one more step... Getting less and less simple by the minute 😂. I may decide to forgoe (sp?) page numbers until @omz is able to come up with a more efficient solution, if it's possible in some future fantasy world. 😉

ccc

Aggravation with pdf merge...

For each client you could create a one page header/footer .pdf file with their name on it. This script would then merge that (background) page with each (foreground) page of their report.

The script below is working flawlessly on my Mac but I can not make it work in Editorial. :-( Perhaps someone else can see what I am missing. Perhaps it is the difference between PyPDF2 v1.22 in Editorial vs. PyPDF2 v1.26 on the Mac.

#!/usr/bin/env python

import PyPDF2

docs_dir = os.path.expanduser('~/Documents')
print(docs_dir)
bg_filename = os.path.join(docs_dir, 'Client_Name_Header_Footer.pdf')
fg_filename = os.path.join(docs_dir, 'Client_Report.pdf')
out_filename = os.path.join(docs_dir, 'Merged.pdf')

with open(bg_filename, 'rb') as bg_file, open(fg_filename, 'rb') as fg_file:
    bg_page = PyPDF2.PdfFileReader(bg_file).getPage(0)
    pdf_out = PyPDF2.PdfFileWriter()
    for page in PyPDF2.PdfFileReader(fg_file).pages:
        if page.extractText():  # Do not copy pages that have no text
            page.mergePage(bg_page)
            pdf_out.addPage(page)
    if pdf_out.getNumPages():
        with open(out_filename, 'wb') as out_file:
            # Caution: All three files MUST be open when write() is called
            pdf_out.write(out_file)
omz

@ccc For some reason, PdfFileReader.pages doesn't seem to work properly. Possible workaround:

fg_reader = PyPDF2.PdfFileReader(fg_file)
fg_pages = [fg_reader.getPage(i) for i in range(fg_reader.getNumPages())]
for page in fg_pages:
    # ...
omz

I've taken @ccc's basic approach, and turned it into a complete workflow:

http://www.editorial-workflows.com/workflow/5807752689483776/zRswEIOv3yY

The core of this is a custom (Python) action that takes a PDF (generated by the standard "HTML to PDF" action), and adds header/footer text on every page. The text can include regular workflow variables (client name, date...) and page numbers – {p} is automatically replaced by the current page number, and {n} is replaced by the total number of pages.

A couple of other things, like font size, margins etc. are also easily configurable by editing the workflow.

NikkiSchwartzVB

Yes, please! And thank you?

I had assumed this was a lost cause.

It'll take me a few days to check this out and implement it.

I'm planning on trying to simplify some of the workflow I've created on the directory as well... In an effort to contribute to the community, FWIW.

The workflows I've put together to write this report have worked well. Albeit with some bugs, but that is to be expected.

Thank you all, again, so much!

ccc

@omz Congratulations! This custom action is really nicely done! Thanks.

NikkiSchwartzVB

@omz one small issue... I can't get the output file name parameter to accept a variable... It's not a huge deal, but it would be convenient.

(I should add, this whole thing is gorgeous and . And the pdf looks great!)

ccc

@NikkiSchwartzVB Did @omz's PDF with page numbers workflow do what you wanted? To me, it seemed like all the right stuff...

NikkiSchwartzVB

I had a chance to play with it last night. It's fantastic.

There is a weird character that shows up at the end of the header text, a black box. But it's not a deal breaker.

Otherwise, it's perfect.

ccc

weird character that shows up at the end of the header text, a black box

My bet is that is a carriage return (\n) at the end of your header text.
If your header text ends in a variable then be sure to .rstrip() the text before putting it into that variable.
In any case, make sure that the last character in the header text field is not \n.

omz

I've updated the workflow to remove leading and trailing line breaks from header/footer text, as they are indeed rendered as a black box (multiple lines are not supported at all, sorry).

Depending on how you generate the header text, it might also be easy to avoid having it end with a line break.

NikkiSchwartzVB

@omz thanks again! It's overall fan friggin tastic. :)

I'll play with this soon