Giter Site home page Giter Site logo

lazypress's Introduction

Logo

lazypress

Convert HTML pages to PDF looking just like they would render in the browser.

Under-the-hood, lazypress is using Google Chrome (via chromedp) to load the HTML so the PDF looks just like if you would print it from the browser.

It also comes with a built-in HTML sanitizer (uses bluemonday).

You can use this code as a library, or you can run it as a server which will spit out PDFs when you send HTML to it.

Features ๐Ÿš€

  • Sanitize HTML to remove potentially malicious code
  • Tweak parameters (landscape/portrait, page size, content scale, margins, etc. )to get a PDF like you want it
  • Add custom header and footer
  • Save to local file or return it as PDF to be downloaded (Content-type: application/pdf)
  • Run with your own Chrome or within a Docker container
  • Use as server if you want just a turnkey solution or as a Go library to include it in your applications

How to use โš™๏ธ

As a server

Start the server with:

lazypress

By default, the server is listening on port 3444. You can change the port by running:

lazypress --port PORT

You can also pass a custom path for Google chrome:

lazypress --chrome CHROME_PATH

Once the server is started, you can send POST requests to the /convert endpoint.

Request

  • Method: POST
  • Content-type: text/plain or text/html

You can tweak the settings of the PDF and decide the output location by passing specific query parameters.

Unfortunately, at least until I find a solution (or you do and send a PR ๐Ÿ˜‰), these query parameters are case sensitive.

Query parameters:

  • sanitize
    • If true, the server will clean up the HTML to remove potentially malicious code.
    • options: true | none
    • default: none
  • output
    • Specify where to output the generated PDF
    • options: file | download | none
    • default: download
  • filename
    • If output is set to "file", this allows you to choose a file name for the PDF (I might remove this due to security concerns)
  • landscape
    • Paper orientation
    • options: true | false
    • default: false
  • displayHeaderFooter
    • Display header and footer
    • options: true | false
    • default: false
  • printBackground
    • Print background graphics
    • options: true | false
    • default: false
  • scale
    • Scale of the webpage rendering (float)
    • default: 1
  • paperWidth
    • Paper width in inches (float)
    • default: 8.5
  • paperHeight
    • Paper height in inches (float)
    • default: 11
  • marginTop
    • Top margin in inches (float)
    • default: 0.4
  • marginBottom
    • Bottom margin in inches (float)
    • default: 0.4
  • marginLeft
    • Left margin in inches (float)
    • default: 0.4
  • marginRight
    • Right margin in inches (float)
    • default: 0.4
  • pageRanges
    • Paper ranges to print, one based, e.g., '1-5, 8, 11-13'. Pages are printed in the document order, not in the order specified, and no more than once. Defaults to empty string, which implies the entire document is printed. The page numbers are quietly capped to actual page count of the document, and ranges beyond the end of the document are ignored. If this results in no pages to print, an error is reported. It is an error to specify a range with start greater than end.
  • headerTemplate
    • HTML template for the print header. Should be valid HTML markup with following classes used to inject printing values into them: - date: formatted print date - title: document title - url: document location - pageNumber: current page number - totalPages: total pages in the document For example, <span class=title></span> would generate span containing the title.
  • footerTemplate
    • HTML template for the print footer. Should use the same format as the headerTemplate.
  • preferCSSPageSize
    • Whether or not to prefer page size as defined by css. Defaults to false, in which case the content will be scaled to fit the paper size.

As a library

Refer to the GoDoc.

FAQ ๐Ÿค”

Can I use it as serverless (e.g. with AWS Lambda)?

I'm working on it. Check the branch feature/serverless.

Contributions are more than welcome!

Can I have the PDF sent via email instead?

I'm working on it. If you want to implement it yourself, please add the implementation in the LoadSettings function.

Can I have the PDF loaded on S3 instead?

Please, refer to the previous question.

Acknowledgements ๐Ÿ™๐Ÿผ

Contributing ๐Ÿค๐Ÿผ

Contributions are always welcome! Just send a PR and after a review, I will be glad to merge your changes!

lazypress's People

Contributors

alexferrari88 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

fuadarradhi

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.