• Stars
    star
    406
  • Rank 106,421 (Top 3 %)
  • Language
    Elixir
  • License
    Apache License 2.0
  • Created almost 5 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Convenient HTML to PDF/A rendering library for Elixir based on Chrome & Ghostscript

CircleCI Module Version Hex Docs Total Download License Last Updated

ChromicPDF is a HTML-to-PDF renderer for Elixir, based on headless Chrome.

Features

  • Node-free: In contrast to many other packages, it does not use puppeteer, and hence does not require Node.js. It communicates directly with Chrome's DevTools API over pipes, offering the same performance as puppeteer, if not better.
  • Header/Footer: Using the DevTools API allows to apply the full set of options of the printToPDF function. Most notably, it supports header and footer HTML templates.
  • PDF/A: It can convert printed files to PDF/A using Ghostscript. Converted files pass the verapdf validator.

Requirements

  • Chromium or Chrome
  • Ghostscript (optional, for PDF/A support and concatenation of multiple sources)

ChromicPDF is tested in the following configurations:

Elixir Erlang/OTP Distribution Chromium Ghostscript
1.15.7 26.2 Alpine 3.18 119.0.6045.159 10.02.0
1.14.5 25.3.1 Alpine 3.17 112.0.5615.165 10.01.1
1.14.0 25.1 Alpine 3.16 102.0.5005.182 9.56.1
1.14.0 25.1 Debian Buster 90.0.4430.212-1 9.27
1.11.4 22.3.4.26 Debian Buster 90.0.4430.212-1 9.27

Installation

ChromicPDF is a supervision tree (rather than an application). You will need to inject it into the supervision tree of your application. First, add ChromicPDF to your runtime dependencies:

def deps do
  [
    {:chromic_pdf, "~> 1.15"}
  ]
end

Next, start ChromicPDF as part of your application:

# lib/my_app/application.ex
def MyApp.Application do
  def start(_type, _args) do
    children = [
      # other apps...
      ChromicPDF
    ]

    Supervisor.start_link(children, strategy: :one_for_one, name: MyApp.Supervisor)
  end
end

Usage

Main API

Here's how you generate a PDF from an external URL and store it in the local filesystem.

# Prints a local HTML file to PDF.
ChromicPDF.print_to_pdf({:url, "https://example.net"}, output: "example.pdf")

The next example shows how to print a local HTML file to PDF/A, as well as the use of a callback function that receives the generated PDF as path to a temporary file.

ChromicPDF.print_to_pdfa({:url, "file:///example.html"}, output: fn pdf ->
  # Send pdf via mail, upload to S3, ...
end)

Template API

ChromicPDF.Template contains additional functionality for controlling page dimensions of your PDF.

[content: "<p>Hello Template</p>", size: :a4]
|> ChromicPDF.Template.source_and_options()
|> ChromicPDF.print_to_pdf()

Multiple sources

Multiple sources can be automatically concatenated using Ghostscript.

ChromicPDF.print_to_pdf([{:html, "page 1"}, {:html, "page 2"}], output: "joined.pdf")

Examples

  • There is an outdated example of how to integrate ChromicPDF in a Phoenix application, see examples/phoenix.

Development

This should get you started:

mix deps.get
mix test

For running the full suite of integration tests, please install and have in your $PATH:

  • verapdf
  • For pdfinfo and pdftotext, you need poppler-utils (most Linux distributions) or Xpdf (OSX)
  • For the odd ZUGFeRD test in zugferd_test.exs, you need to download ZUV and set the $ZUV_JAR environment variable.

Acknowledgements

Copyright and License

Copyright (c) 2019โ€“2023 Bitcrowd GmbH

Licensed under the Apache License 2.0. See LICENSE file for details.

More Repositories

1

carbonite

Audit trails for Elixir/PostgreSQL based on triggers
Elixir
163
star
2

sshkit.ex

An Elixir toolkit for performing tasks on one or more servers, built on top of Erlangโ€™s SSH application.
Elixir
147
star
3

tickety-tick

A browser extension that helps you name branches and write better commit messages
TypeScript
57
star
4

bitstyles

A complete design system, with base styles and components, built in Sass using CSS variables
MDX
15
star
5

herodot

Track your work with your git activity.
Ruby
12
star
6

rails-monitoring

A rails engine to monitor sidekiq internals.
Ruby
11
star
7

bitcrowd_ecto

A collection of useful additions to ecto we use in our projects.
Elixir
11
star
8

mdast-util-from-adf

Convert from Atlassian Document Format to Markdown Abstract Syntax Tree
TypeScript
10
star
9

bitstyles_phoenix

A collection of Elixir phoenix helpers for bitstyles
Elixir
10
star
10

rag_time

๐Ÿ’ Example code for a blog post series about using a RAG system on a local codebase.
Python
7
star
11

javascript

Bitcrowd JavaScript Guide
6
star
12

sql_to_csv_stream

DEPRECATED - A shortcut to the COPY command from PostgreSQL. Give it SQL and get an Enumerator for the CSV/JSON that comes out of it.
Ruby
6
star
13

rubocop-bitcrowd

The bitcrowd rubocop.yml as a gem.
Ruby
6
star
14

frontend

How we do frontend things at bitcrowd
JavaScript
5
star
15

tickety-tick-formatter

[Deprecated]: Format your tickets into git-digestable branch names and commit messages
TypeScript
2
star
16

elixir-sftp-server-example

Example of an SFTP server implemtation in Elixir / Erlang as a reference to a corrensponding post on the bitcrowd blog.
Elixir
2
star