docs-to-pdf
docs-to-pdf copied to clipboard
bookmarks
Can I support generating PDF bookmarks?
Hello @zhljt,
Could you elaborate on the bookmarks you want on the PDF ? Do you have some visual example to share please ? I would be happy to add some features.
Effect like this
This PDF is derived from the conversion of Word files。
1111.pdf
I want the PDF bookmarks to be consistent with the sidebar on the webpage (https://v2.vuepress.vuejs.org/guide/)
Do you think it's okay?
It is not an easy thing. Pupperteer is not able to do it on its own. Here is a solution for marp (a markdown presentation framework) base on pdf-lib. Does not look too easy though ;) https://github.com/marp-team/marp-cli/pull/479 However, this would be better than the not that beautiful TOC.
The plan is following (adapted from the linked PR):
- (docs-to-pdf) mark heading tokens with unique key, to make recognizable heading items in browser. I guess this is already done by the TOC!
- (Puppeteer) Before converting HTML to PDF, the browser finds marked heading items and resolves the rendered positions and displayed texts: offsetLeft, offsetTop, and textContent.
- (pdf-lib) Postprocess the output PDF to assign document outlines, from the result of 1 and 2.
I just found the repo html-export-pdf-cli. Looks like they are doing a great job for static html files. Might be good to learn from them. Anyway, they also have an outline generation also based on pdf-lib. It looks like it is easyer adaptable for this project.
I am also tempted to experiment with their library directly to docusaurus. They do not have the exclude selectors and pagination feature, however the fundation looks very good.
Edit: they have a plugin for viewpress. looks like it would not be that hard to adapt it to docusaurus: https://github.com/condorheroblog/vitepress-export-pdf
@codingluke thank you for all those input. You made me discover both solutions 👍 I draft a first solution, but nothing relevant for now. I will have a look at the last proposition and try to adopt it here
Small update: here is the PR #223 > It's generating the outline and the PDF, but right now the PDF is not showing (at least when generating from my Mac for testing). Still trying to find out why, very strange because I almost didn't make any change from the code of @condorheroblog
It took me two and a half hours to debug.
Just as I was about to leave work, I realized it was an error caused by incorrect usage of pdf-lib and page.pdf. The correct code is as follows:
const pdfExportOptions = {
// 1. delete
path: this.options.outputPDFFilename ?? 'output.pdf',
format: this.options.paperFormat,
margin: this.options.pdfMargin ?? {
top: 32,
right: 32,
bottom: 32,
left: 32,
},
headerTemplate: this.options.headerTemplate,
footerTemplate: this.options.footerTemplate,
displayHeaderFooter: !!(
this.options.headerTemplate || this.options.footerTemplate
),
printBackground: true,
timeout: 0,
};
const pdf = await page.pdf(pdfExportOptions).catch((err) => {
console.error(chalk.red(err));
throw new Error(err);
});
const pdfDoc = await PDFDocument.load(pdf);
setOutline(pdfDoc, outline, true);
// 2 get this
const buffer = await pdfDoc.save();
// 3. write
writeFileSync(this.options.outputPDFFilename ?? 'output.pdf', buffer);
You rock @condorheroblog 🥇
Hello @jean-humann , i am fork this project and merge this branch with master to test, just add this parameter '--puppeteerArgs=--no-sandbox' to the command line and it worked without any problems and generate pdf with outlines. Please do the test, and if it works, make this feature available to us. Thanks
any progress?