Skip to content

Serverless PDF generator for Docusaurus and other SSGs

License

Notifications You must be signed in to change notification settings

ksmarty/pdf-press

 
 

Repository files navigation

📌 About

This is a PDF generator from document website such as docusaurus, vuepress, and mkdocs built for serverless applications. The code is forked from @KohheePeace's mr-pdf.

⚡ Usage

import pdf from "pdf-press";

export default async function (req, res) {
	const file = await pdf({
		initialDocURLs: ["https://example.com"],
		contentSelector: "article",
		paginationSelector: "li > a",
		coverTitle: "A Very Cool Title",
		coverSub: "An equally interesting subtitle",
	});

	res.send(file);
}

🍗 Options

Property Default Required Description
initialDocURLs: string[] - Yes URL(s) to start generating PDF from.
contentSelector: string - Yes CSS selector used to find the part of main content.
paginationSelector: string - Yes CSS selector used to find next page. To only select one page, use a random word. E.g. "nope".
excludeURLs: string[] - No URL(s) to be excluded from PDF.
excludeSelectors: string[] - No Element(s) to be excluded. Each element is its own selector.
cssStyle: string - No CSS styles to modify the PDF. Project owners can also use @media print { } add CSS for the PDF.
outputPDFFilename: string pdf-press.pdf No Name of the PDF file.
pdfMargin: puppeteer.PDFOptions['margin'] { top: 32, right: 32, bottom: 32, left: 32 } No Margin around the PDF file.
pdfFormat: puppeteer.PDFOptions['format'] - No PDF format. See options.
puppeteerArgs: puppeteer.LaunchOptions See Below No Options to set for the headless browser on launch. More info here
coverTitle: string - No Title for the PDF cover.
coverSub: string - No Subtitle the for PDF cover. Add <br/> tags for multiple lines.
coverImage: string - No URI to image for the PDF cover.
disableTOC: boolean false No Disable table of contents.

Launch Options

The default settings are listed below. chrome refers to chrome-aws-lambda; a version of chrome intended for use in serverless applications. Version 7 of both puppeteer and chrome-aws-lambda are being used as they are just small enough to fit into AWS's requirements (Vercel uses AWS on the backend). See all available options here

{
  args: chrome.args,
  executablePath: await chrome.executablePath,
  headless: chrome.headless,
}

CSS Classes

  • .pdf-cover
  • .cover-title
  • .cover-subtitle
  • .cover-img

The structure for the cover page is as follows:

  <div
    class="pdf-cover"
    style="
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        height: 100vh;
        page-break-after: always;  
        text-align: center;">

    <h1 class="cover-title"> Title </h1>
    <h3 class="cover-subtitle"> Subtitle </h3>
    <img 
        class="cover-img" 
        src="example.png"
        width="140"
        height="140"/>
  </div>`

📄 How this plugin works

This plugin uses puppeteer to make PDF of the document website.

diagram

🎉 Thanks

This repo's code is coming from https://github.com/KohheePeace/docusaurus-pdf.

Thanks for awesome code made by @maxarndt and @aloisklink.

@bojl approach to make TOC was awesome and breakthrough.

About

Serverless PDF generator for Docusaurus and other SSGs

Resources

License

Stars

Watchers

Forks

Languages

  • TypeScript 100.0%