docs-to-pdf icon indicating copy to clipboard operation
docs-to-pdf copied to clipboard

bookmarks

Open zhljt opened this issue 2 years ago • 10 comments
trafficstars

Can I support generating PDF bookmarks?

zhljt avatar Jul 25 '23 04:07 zhljt

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.

jean-humann avatar Jul 25 '23 09:07 jean-humann

Effect like this 企业微信截图_16907947786542 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/) 企业微信截图_16907948561874

Do you think it's okay?

zhljt avatar Jul 31 '23 09:07 zhljt

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):

  1. (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!
  2. (Puppeteer) Before converting HTML to PDF, the browser finds marked heading items and resolves the rendered positions and displayed texts: offsetLeft, offsetTop, and textContent.
  3. (pdf-lib) Postprocess the output PDF to assign document outlines, from the result of 1 and 2.

codingluke avatar Aug 08 '23 18:08 codingluke

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 avatar Aug 13 '23 07:08 codingluke

@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

jean-humann avatar Aug 13 '23 07:08 jean-humann

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

jean-humann avatar Aug 23 '23 07:08 jean-humann

It took me two and a half hours to debug.

image

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);

condorheroblog avatar Aug 23 '23 12:08 condorheroblog

You rock @condorheroblog 🥇

jean-humann avatar Aug 23 '23 14:08 jean-humann

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 Captura de tela 2024-06-19 173709

westorres9 avatar Jun 19 '24 20:06 westorres9

any progress?

pfdgithub avatar Mar 13 '25 09:03 pfdgithub