Replies: 3 comments 1 reply
-
Hello @gnapse 👋 There is no built in functionality alas, but I guess your last suggestion would actually work! |
Beta Was this translation helpful? Give feedback.
-
After having posted it, I thought a bit more, and I doubt that it will work. I can probably achieve having a first page with some links to the chapters. But I doubt that a PDF reader would recognize this as a proper semantic table of content that it would show in the sidebar, for instance. I'll investigate a bit about how these tables of content work in PDF. But it sounds like something that a PDF generator would need to add intentionally. And perhaps a Chromium-to-PDF tool is not the right tool for the job. We'll see. |
Beta Was this translation helpful? Give feedback.
-
I've found that this is indeed a built-in feature of PDF. There are two aspects here: let's forget for now about the initial PDF page with a visible table of content, and let's focus on the internal table of content that PDF reader apps will show in a sidebar or similar UI. These are technically called bookmarks. I found several resources online about ways to add them programmatically to generated PDFs. It seems to be supported by pdftk (which I believe Gotenberg uses internally). But I found the information about it more confusing and harder to follow. For now, I found a way to do this outside of Gotenberg, using PyPDF2. Here's an outline of a script that takes care of concatenating multiple PDFs, and in the process, it adds a bookmark to the first page of each of the original PDFs: import PyPDF2
def merge_pdfs(pdf_files, output_file):
pdf_writer = PyPDF2.PdfWriter()
bookmark_page = 0
for pdf_file in pdf_files:
pdf_reader = PyPDF2.PdfReader(pdf_file)
num_pages = len(pdf_reader.pages)
bookmark_title = pdf_reader.metadata.title
pdf_writer.add_outline_item(bookmark_title, bookmark_page)
# Add pages from current PDF to the output
for page in range(num_pages):
pdf_writer.add_page(pdf_reader.pages[page])
# Update bookmark page for the next PDF
bookmark_page += num_pages
# Write the combined PDF with bookmarks
with open(output_file, 'wb') as output:
pdf_writer.write(output)
# Example usage: Merge three input PDFs into a single output PDF
input_pdfs = ['1.pdf', '2.pdf', '3.pdf']
output_pdf = 'output.pdf'
merge_pdfs(input_pdfs, output_pdf) It's less convenient than having the functionality in Goternberg, but I wonder if it's reasonable to make this a feature request that could be implemented in the future. I can open an issue to request, and I'd even be willing to contemplate helping develop it (though I have never developed for Gotenberg or used Go even, but I could give it a try as a personal challenge!) Let me know. |
Beta Was this translation helpful? Give feedback.
-
Is there a way to generate a PDF with a table of content? My use-case is similar to merging multiple PDFs. Imagine I have one PDF per chapter of a book. I want to merge them, but I also want to generate pages at the front with a table of content. One that would have links to each chapter, and that would show also in certain PDF reading apps as a UI outside the PDF (usually in the sidebar) for users to more easily navigate to chapters in the document.
Is this something that Gotenberg can help with? Or is it something for which I can generate HTML myself and have Gotenberg generate the PDF (say, providing my own HTML structure of everything, assuming I can have the individual chapters in HTML initially).
Beta Was this translation helpful? Give feedback.
All reactions