print2pdf-poster

Print 2 PDF

Docker image to export HTML pages to PDF wrapping Puppeteer with a simple REST API.

Print to PDF logo

Print to PDF • A REST API to generate PDFs from Web pages.

Source link Authors link License


Just make your HTML document available at a public URL, and generate a PDF out of it adjusting options like paper or margins, without installing any paid library or software.

Resulting PDFs are uploaded to an S3 bucket where they can be downloaded.

As a Serverless application

You can deploy this repository as a serverless application using an AWS CloudFormation Template to create an AWS API Gateway that invokes Lambda functions to serve requests.

Launch this stack on AWS

Endpoints

POST /print

Export a publicly available HTML page as a PDF.

Headers:

  • Content-Type: application/json

Body parameters:

  • url (required): the publicly available URL of the page you want to print as PDF.
  • file_name (required): friendly file name for the exported PDF. It MUST end with .pdf and MUST NOT contain any slash, non-ASCII or non-printable character (except plain spaces).
  • media (optional): media to be emulated when printing to PDF. Can be either "print" or "screen". Defaults to "print".
  • format (optional): paper format of PDF. Can be one of "Letter", "Legal", "Tabload", "Ledger", "A0", "A1", "A2", "A3", "A4", "A5". Defaults to "A4".
  • background (optional): enable or disable background graphics in printed PDF. Defaults to true.
  • layout (optional): page layout of printed PDF. Can be either "portrait" or "landscape". Defaults to "portrait".
  • margin (optional): page margin of printed PDF. Is an object containing four keys (top, bottom, left, right) with values expressed as distance units (e.g.: "1cm").
  • scale (optional): print scale. Can be any positive decimal number. Defaults to 1 (1 means 100%, 0.5 means 50%, 2 means 200%, …).

Examples:

POST /print HTTP/1.1
Content-Type: application/json

{
    "url": "http://example.com/my-page/",
    "file_name": "my-friendly-file-name.pdf"
}
POST /print HTTP/1.1
Content-Type: application/json

{
    "url": "http://example.com/my-page/",
    "file_name": "my-friendly-file-name.pdf",
    "media": "screen",
    "format": "A5",
    "background": false,
    "layout": "landscape",
    "margin": {
        "top": "1cm",
        "bottom": "1.5cm",
        "left": "1cm",
        "right": "1cm",
    },
    "scale": 0.94
}

Responses:

  • 200 OK

    HTTP/1.1 200 OK
     Content-Type: application/json
    
     {
         "url": "https://s3.amazonaws.com/example-bucket/your-file.pdf"
     }
  • 400 Bad Request

    HTTP/1.1 200 OK
     Content-Type: application/json
    
     {
         "status": 400,
         "message": "Bad Request",
         "details": {
             "message": "Some hopefully helpful message"
         }
     }
  • 500 Internal Server Error

    HTTP/1.1 200 OK
     Content-Type: application/json
    
     {
         "status": 500,
         "message": "Internal Server Error",
         "details": {
             "message": "Something really bad happened"
         }
     }
GET /status

Check application status.

Responses:

  • 200 OK

    HTTP/1.1 200 OK
     Content-Type: application/json
    
     {
         "ok": true
     }

Development

All the following instructions assume you have at least NodeJS and Yarn installed.

Start a simulated AWS API Gateway (provided you have AWS SAM Local and Docker installed):

yarn run api-gateway

Validate CloudFormation template (provided you have AWS CLI installed)

make validate

Package CloudFormation template (provided you have AWS CLI and Docker installed)

make layers (this is needed only the first time, then when updating Puppeteer version) make package

Deploy CloudFormation template (provided you have AWS CLI and Docker installed)

make deploy make deploy ENVIRONMENT=Production


License

Print to PDF is released under the MIT license.