It uses Puppeteer and headless Chrome to request an HTML document and create a PDF.
- Simpler that propietary PDF-building syntaxes. Just use HTML & CSS.
- Simple stateless architecture
- Easy to setup
- Originating app server requests a PDF document.
- The request contains the URL of an HTML page.
bakso-doc
uses Puppeteer to request the HTML page.Page.pdf
method is called to generate a PDF document from the HTML page.- The entire PDF document is sent back in the response to the originating server.
bakso-doc
runs synchronously. When a document is requested, the request does not finish until the entire document is sent back. It is meant to be used in a queue system where the length of time to complete a request is not important.
bakso-doc
is agnostic to other parts of the document processing system.
Because Puppeteer/Chrome makes the requests diectly by setting the page URL, only GET requests can be made to the document page. You must pass any parameters in the URL query string.
In the interest of keeping server auth simple and easy to setup, bakso-doc
authorizes requests with JWT tokens. The default is to generate a long-lived token for use on the requesting server.
Optionally, the secret can be shared across services and used to both sign and verify the tokens.
CSS provides a powerful set of media queries and styles to be able to style a document.
@page {
size: A4 landscape;
}
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_media_queries/Printing
https://www.smashingmagazine.com/2011/11/how-to-set-up-a-print-style-sheet/
Clone the repo:
$ git clone https://github.com/awongh/bakso-doc.git && cd bakso-doc
Install deps:
npm i
Generate the secret and set it in the .env
file:
$ touch .env &&
echo "BAKSO_SECRET_KEY=\"$( node -e "console.log(require('crypto').randomBytes(256).toString('base64'));" )\"" >> .env
Generate a requesting token:
$ npm run generate_token <SECRET GOES HERE>
Set the requesting token in the Authorization
request header.
Use cURL to make the request and download the file- create a test file called test.pdf
in the current directory.
$ TEMP_TOKEN=$(npm run generate_token "<YOUR SECRET HERE>" | tail -n 1) &&
curl -X POST -H "Authorization: ${TEMP_TOKEN}" -H "Content-Type: application/json" --output test.pdf -d '{"pdfParams":{"renderUrl":"https://example.com"}}' http://localhost:5003/download/pdf
See the schemas.js file to see the full accepted request params.
See the PDF options as defined by Pupeteer.
Example:
{
"name":"myfile",
"renderUrl":"https://localhost:3000/mydoc",
"pdfOptions":{
"width":"8.5in",
"height":"11in",
"printBackground":true
}
}
For deployment on render.com see the render.com branch.
Error monitoring with Sentry is already setup.
Add your Sentry DSN to the .env
file:
BAKSO_SENTRY_DSN='<YOUR SENTRY DSN>'
Checkout the examples directory.
Express server that creates a customized 8½x11 birthday card.