itamarjp / pdf_reports

:closed_book: Python library and CSS theme to generate PDF reports from HTML/Pug

Home Page:https://edinburgh-genome-foundry.github.io/pdf_reports/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DNA Chisel Logo

PDF_Reports

Travis CI build status

image

PDF Reports (complete documentation here) is a Python library to create nice-looking PDF reports from HTML or Pug templates. It features modern-looking components (via the Semantic UI framework) and provides routines to embed tables or plots in the documents.

Note that only Python 3.x is officially supported, although with the right version of weasyprint the library can also run on 2.x.

Example of use

Your Pug template file template.pug may look like this (see a full example):

#sidebar I am the text in the sidebar.

h1 {{ title }}

.ui.piled.segment
  p Oh hi there ! I am some text in a cool box.

Your Python code will be as follows:

And your final result may look like this (PDF file):

image

See also this example embedding some python code in the template to create figures and tables on the flight.

Other features

Preloading CSS and SCSS

PDF Reports provides a preload_stylesheet method which can be used to load and parse a CSS file. It also works with SCSS files (which will automatically be compiled to CSS) but this requires libsass installed (for instance via pip install libsass). Here is an example:

Using a ReportWriter

The ReportWriter class allows to define default templates, styles, and variable names. It can be used to avoid repeating yourself across your application:

Markdown support

As a feature of PyPugJS, markdown is supported in the Pug templates.

div
  :markdown
    This is some markdown text. Here is a [link](http://example.com/).

    - this is a bullet point list
    - Second item
    - Etc.

PDF tools

Some useful functions for generating reports are available from inside the Pug templates under pdf_tools. For instance, pdf_tools.figure_data() to embed matplotlib images, or pdf_tools.dataframe_to_html() to turn Pandas dataframes into HTML, and style them nicely with Semantic UI. Have a look at the docs, or this example

JupyterPDF

The JupyterPDF class eases report templates writing by embedding PDF files in Jupyter notebooks (using the browser's interactive PDF viewer).

Notes

The core of the library consists of just a few lines of Python, using pypugjs to parse Pug templates, optionally including stylesheets from the Semantic UI CSS framework, and finally calling weasyprint for PDF generation. Please refer to the Weasyprint documentation for the customization of templates. For instance, to customize the page margins and numbering the Weasyprint way, add this to your SCSS code:

@page {
    margin: 1cm 0 2cm 0cm;
    @bottom-center {
        content: "Page " counter(page) " / " counter(pages);
        font-family: 'Lato';
    }
}

Using Semantic UI implies that (1) the Lato font family should be installed on your machine, otherwise the results will look less good, and (2) the first time that write_pdf is called in a Python session, if using the default Semantic UI style, the parsing of the CSS will add a 3-second overhead to the function calls (but there will be no overhead for the next calls in that session).

Installation

You can install the library via PIP

sudo pip install pdf_reports

Alternatively, you can unzip the sources in a folder and type

sudo python setup.py install

Note: on some Debian systems you may need to first install libffi-dev (apt install libffi-dev). The package name may be libffi-devel on some systems.

Note: on macOS, you may need to first install pango with:

` brew install pango`

License = MIT

This open-source software project was originally written at the Edinburgh Genome Foundry by Zulko and released on Github under the MIT licence (¢ Edinburg Genome Foundry). Everyone is welcome to contribute !

About

:closed_book: Python library and CSS theme to generate PDF reports from HTML/Pug

https://edinburgh-genome-foundry.github.io/pdf_reports/

License:Other


Languages

Language:Python 87.8%Language:HTML 6.6%Language:CSS 5.6%