Add page numbers to PDF export

Use case or problem

After exporting large documents to PDF and then printing them, the pages often get shuffled or reordered through normal use. It can be hard to get the pages back into the correct order because there are no page numbers.

Proposed solution

I request an option on the Export to PDF dialog to add the page number to the footer of the printed pages.

Current workaround (optional)

None. I haven’t found any easy way to insert page numbers into the created PDF without damaging the content (for example, pspdftool can scramble embeds when it inserts numbers).

Related feature requests (optional)

The only related post I found was this one asking if there is a way in CSS to add page numbers, with no resolution:

20 Likes

I agree that this should be a core feature, but in the meantime, can you do this with pandoc?

2 Likes

Hi Eleanor, thanks for your comment.

I’ve tried using Pandoc a couple of times, but I’ve struggled to get it to handle embeds of any kind, even using the Pandoc export plugin. Embedded images often come up as broken links, and Excalidraw drawings don’t show up at all.

Of course, it’s likely I just don’t understand how to use it correctly! Maybe there’s an export-to-Pandoc-Markdown step I’m missing.

Craig

2 Likes

I would support this request. I tried to figure out how to do this via pandoc, but I found the normal obsidian export does a better job overall than just using the obsidian-pandoc plugin. It would help to be able to indicate things like a TOC or page numbers or the like than to just have an export to PDF with no options available.

3 Likes

Hi @Craig, I completely agree. I still use pandoc here sometimes, mainly with eisvogel template, but it always messes up with embedded plugins like charts and drawings.

On one hand the core PDF export tool from Obsidian works very well with good visuals but it is not configurable. Pandoc lets us configure almost everything but the current implementation is a little fragile (small changes lead to cryptic error messages) and doesn’t support plugins well (at least on my tests).

So I’d love to see the core PDF export tool supporting headers and footers, for example. Maybe asking for cover and TOC pages is a little too much…

3 Likes

Ditto! I can get Pandoc to generate page numbers but find that embedded figures are poorly managed.

I think Obsidians pdf export works nicely but needs tweaking and some more options. With Css you can configure Obsidians pdf export however you want, but for page numbers i would not know
Adding page numbers would not hurt.

3 Likes

I want to echo this!

For the meantime I found this workaround using VSCodium and the Markdown PDF extension. It works fine BUT it does not handle ==highlights==. Does anybody know, how to work around this?

I know that ==highlights== work just fine in the new Pandocs3 version… however I have other issues there with german Umlaute inside strikethroughs ( äöü)…

Any idea how I can make all working while waiting for the Obisidan functionality?

1 Like

I used this tool today to add page numbers to an Obsidian-exported PDF. Worked perfectly, other than limited font options. Easy workaround until the devs add this feature.

4 Likes

Suggestion: The header and footer should have placeholders for texts, date, filename with or without path, page x from y.

Don’t forget the translations:

Seite x von y

2 Likes

If you are on Linux you may use a combination of command line tools as described in Numbering the pages of a PDF

1 Like

That was very helpful! I was able to use that link to cobble together a bash script to add page numbers to the header of an existing PDF. It seems to work even when the PDF has embeds, images, and Mermaid diagrams.

I posted my first draft of the script below.

Many thanks for the suggestion!

add-page-numbers-to-pdf.sh

# Writes page-numbered output to filename-with-page-numbers.pdf in the same
# directory as the original file.

# Usage:
# add-page-numbers-to-pdf.sh path/to/filename/filename.pdf

# Requires the following libraries:
# sudo apt install enscript pdftk podpdf

# Adapted from Andrés Aravena's solution:
# https://anaraven.bitbucket.io/blog/2018/numbering-pages-of-a-pdf.html

# Thanks to senoussi on the Obsidian forum:
# https://forum.obsidian.md/t/add-page-numbers-to-pdf-export/32525/11

######################################################################
# CUSTOMIZATION SETTINGS
######################################################################

# To change the style, set PAGE_NUMBER_STYLE to the output you want.
PAGE_NUMBER_STYLE_FULL='Page $% of $='
PAGE_NUMBER_STYLE_MINIMAL='$%/$='
PAGE_NUMBER_STYLE=${PAGE_NUMBER_STYLE_MINIMAL}

# To change the output file suffix, modify OUTPUT_FILE_SUFFIX
OUTPUT_FILE_SUFFIX="-with-page-numbers.pdf"

######################################################################
# MAIN SCRIPT
######################################################################

# Calculate number of pages
NUMBER_OF_PAGES=$(pdftk "$1" dump_data | grep "NumberOfPages" | cut -d":" -f2)

# Run PDF through enscript -> pd2pdf -> pdftk
enscript \
    -L1 \
    -b"||${PAGE_NUMBER_STYLE}" \
    -o- < <(for i in $(seq "${NUMBER_OF_PAGES}"); do echo; done) \
| ps2pdf - \
| pdftk "$1" multistamp - output "${1%.pdf}${OUTPUT_FILE_SUFFIX}"
1 Like

Hi Craig,
glad that I could help!

As in my tests it overwrote some text in the header of my A4 pages I tried the proposed solution in the section “Printing footers instead of headers”.
I have put the enscript configuration / customisation as listed in Printing footers using enscript into ~/.enscript/footer.hdr and changed the enscript part to use

enscript --fancy-header=footer -L1 -b’||’ --footer ‘|Seite $% von $=|’ …

and this way it looks perfect to me!
Regards,
Dirk

1 Like

After much trial and error I finally succeeded in inserting page numbers with CSS!

First, please create the following snippet of CSS.

div.page-break:after {
  content: ">> Page Break";
}

@media print {

  body {
    counter-reset: page;
  }

  div.page-break {
    page-break-before: always;
    position: relative;
  }

  div.page-break:after {
    counter-increment: page;
    content:"Page " counter(page);
    position: absolute;
    top: 10mm;
    right: 10mm;
  }
}

Then insert the page numbers with the following tags.

<div class="page-break"></div>
1 Like