Difference between revisions of "Research/Web-to-print/WeasyPrint"

From Publication Station
Line 70: Line 70:
* Can be used as a Python library or as a standalone program
* Can be used as a Python library or as a standalone program
* Free software (BSD license)
* Free software (BSD license)
 
* Clean documentation
===negative aspects===
===negative aspects===
* Can be difficult to install, due to its dependencies
* Can be difficult to install, due to its dependencies
* It does not support CSS custom fonts
* It does not support CSS custom fonts
* No support for folded formats, such as booklets: no mechanism for imposition
* No support for folded formats, such as booklets: no mechanism for imposition
----
=What is Weasy Print=
Weasy Print (WP) is






=Testing Weasy Print=
=Testing Weasy Print=
At the first glance Weasy Print sounds promising, so why not try to see how well does it performs  conversions from HTML to PDF and what CSS rules does it apply consistently.
At the first glance Weasy Print sounds promising, so why not try to see how well does it performs  conversions from HTML to PDF?


The following PDF results from the conversion of the [http://beyond-social.org/ Beyond Social's] article [http://beyond-social.org/articles/Eiland_van_Brienenoord.html ''Eiland van Brienenoord''] onto a PDF using Weasy Print.  
The following PDF results from the conversion of the [http://beyond-social.org/ Beyond Social's] article [http://beyond-social.org/articles/Eiland_van_Brienenoord.html ''Eiland van Brienenoord''] onto a PDF using Weasy Print.  
Line 95: Line 87:
[[File:web2print-BS07.pdf]]
[[File:web2print-BS07.pdf]]


===Running===
==Running==
The standalone command <code>weasyprint</code> can produce a PDF, simply with the instructions:
The standalone command <code>weasyprint</code> can produce a PDF, simply with the instructions:
  weasyprint EilandvanBrienenoord.html -s style.css ouput.pdf
  weasyprint EilandvanBrienenoord.html -s style.css ouput.pdf
Line 104: Line 96:
* <code>ouput.pdf</code> - the resulting PDF
* <code>ouput.pdf</code> - the resulting PDF


=== @page ===
== @page ==
@page CSS rule that determines orientation and page size is successfully rendered in the PDF.


===  @page left @page right @bottom-right ===
<source lang="css">
@page {
size: A5 portrait;
}
</source>


==  @page left @page right ==
So are specific option for the left and right pages, such as the margin sizes, which have to alternate in order to produce a bound work.


=== page counter===
<source lang="css">
@page:right {
  margin-left: 3cm; /*inner margin*/
  margin-right:1cm; /*outer margin*/
}


=== CSS Custom Fonts ===
@page:left {
  margin-right: 3cm; /*inner margin*/
  margin-left:1cm; /*outer margin*/
}
</source>


* @page rule - define the page dimensions prototype02 - works
[[File:weasyprint-margins.png]]
* @media print - weasyprint follows @media print
* @page left; @page right - protype04
** @bottom-right - prototype05


* page number counter - prototype06
== @bottom ==
* CUSTOM FONTS - font-face is not supported. But WeasyPrint should pick up any
Weasy-print also applies consistently  <code>@bottom</code> rules, including page counting.
font that Pango/fontconfig are able to find.


examples of works with weasy
<source lang="css">
  @bottom-right {
    margin: 10pt 0 30pt 0;
    border-top: .25pt solid #FF05F6;
    content: "Testing WeasyPrint";
    font-size: 6pt;
    color: #00FFF2;
  }
 
  @bottom-center {
    margin: 10pt 0 30pt 0;
    content: counter(page);
    font-size: 6pt;
  }
</source>


try table


== CSS Custom Fonts ==
Weasy Print does not support CSS's (<code>@font-face</code>) rule.


To use custom fonts it relies on Pango library <ref name"pango">http://www.pango.org/</ref> for text layout.


A discussion on the use of custom fonts can be followed on Weasy Print's [https://github.com/Kozea/WeasyPrint/issues/231 Github issue]
== Imposition ==
There appears to be no mechanism that performs page impositions.
Although it is possible to perform imposition over the Weasy Print-generated PDF with software such as [http://kjo.herbesfolles.org/bookletimposer/ BookletImposer], [http://jpdftweak.sourceforge.net/ jPDF Tweak], [https://github.com/raffamaiden/pdftools pdftool], or [https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/ PDFtk].
=== more tests ===
These tests and prototypes are far from exhaustive.
It is still unclear weather Weasy Print, CSS and HTML can produce quality PDFs, ready to send to a printer in order to produce paper-bound publication.
The results are promising.
Yet more experimentation, prototypes, and feedback is needed in order to make a informed critique.
And contributions to [https://github.com/Kozea/WeasyPrint/ Weasy Print], will only help.


==CSS style-sheet==
==CSS style-sheet==
Line 152: Line 190:
     font-size: 6pt;
     font-size: 6pt;
   }
   }
}
}


Line 172: Line 209:
     font-size: 6pt;
     font-size: 6pt;
   }
   }
}
}
   
   
@page:first {
@page:first {
 
   @bottom-center { content: ""; }
 
   @bottom-right { content: ""; }
   @bottom-center {
    content: "";
  }
    
    @bottom-right {
    content: "";
  }
 
 
}
}


Line 208: Line 234:
} /*title*/
} /*title*/


h1,h2,h3,h4,h5 {color:#FF05F6;
h1,h2,h3,h4,h5 {color:#FF05F6; }
}




Line 217: Line 242:
}
}
    
    
img{
img{ width: 6cm; }
  width: 6cm;
}


figcaption { font-size:0.8em;
figcaption { font-size:0.8em;

Revision as of 10:45, 26 November 2015

Print in hybrid publishing workflows

Print products = bottleneck in hybrid publishing workflows

Screens in pages represent different paradigms

  • Instead of reflowable text, continuous space, variable screen size
  • Page: a fixed space

The translation of layouts for screen to page layouts, within is not trivial.

HTML-to-print Tools

LaTex

A type-setting/document preparation language, focused on producing typographicaly correct PDFs.

positive aspects

  • LaTex is a markup language;
  • Pandoc converts seamlessly to LaTex.
  • Supports page numbers, hyphenation, bibliography, references, hyperlinks, etc, etc...
  • Can produce more experimental and generative outputs. [1].

negative aspects

  • Outputs are by default look academic, although this can be changed
  • Use is outmoded
  • Styling is defined by packages imported into the document, which is very different and incompatible with CSS.
  • A difficult tool to work with, let alone to teach.


Browser built-in pdf rendering

Current browsers have built-in pdf rendering engines. By using the browser function «Print to file» and CSS rules for print, print layouts can be created from HTML files.

positive aspects

  • relies on the same set of technologies - HTML, CSS and JS - as other hybrid publishing formats: webpages, ePubs, apps
  • it is simple, easily to teach
  • CSS includes page specific options: @media page rule, and the pseudo-classes :first :left :right @bottom-left

negative aspects

  • works poorly and inconsistently
  • requires a lot of trial-and-error to achieve the desired output, with results differing widely across browsers and versions

* difficult to avoid page breaks breaking and hiding content.

show example

Desktop publishing software

Software such as Scribus and inDesign can be incorporated into an HP workflow.

Scribus can import HTML files and inDesign ICML(XML)files. Both formats keep their structure when imported and can remain updatable.

positive aspects

  • allows direct feedback between operations and their effects
  • graphic designers are familiar their workflows
  • can produce complex PDFs with impositions, hyphenation, page numbers. etc
  • can intervene in almost all aspects of the document

negative aspects

  • to keep the content updatable operations (in inDesign) interventions on content are not possible
  • complex structures, like large tables, are difficult to handle, while keeping content updatable (in inDesign)


Weasy Print

Weasy Print (WP)[2] is a visual rendering engine for HTML and CSS that can export to PDF.

Its CSS layout engine is written in Python, but employs several libraries to render the HTML and CSS onto a PDF.


positive aspects

  • Uses HTML and CSS to layout the PDF
  • Supports features like page size, page numbering, hyphenation in several languages [3]
  • Resulting PDFs can achieve high level of complexity, with simple instructions
  • Simple and easy to use
  • Can be used as a Python library or as a standalone program
  • Free software (BSD license)
  • Clean documentation

negative aspects

  • Can be difficult to install, due to its dependencies
  • It does not support CSS custom fonts
  • No support for folded formats, such as booklets: no mechanism for imposition


Testing Weasy Print

At the first glance Weasy Print sounds promising, so why not try to see how well does it performs conversions from HTML to PDF?

The following PDF results from the conversion of the Beyond Social's article Eiland van Brienenoord onto a PDF using Weasy Print.

The CSS style-sheet that gave origin the PDF is located at the of this page

Web2print-BS07.pdf

Running

The standalone command weasyprint can produce a PDF, simply with the instructions:

weasyprint EilandvanBrienenoord.html -s style.css ouput.pdf

Where:

  • EilandvanBrienenoord.html - is the souce HTML file (could also be a URL)
  • -s - is the option for a CSS stylesheet
  • ouput.pdf - the resulting PDF

@page

@page CSS rule that determines orientation and page size is successfully rendered in the PDF.

@page {
size: A5 portrait;
}

@page left @page right

So are specific option for the left and right pages, such as the margin sizes, which have to alternate in order to produce a bound work.

@page:right {
  margin-left: 3cm; /*inner margin*/
  margin-right:1cm; /*outer margin*/ 
}

@page:left {
  margin-right: 3cm; /*inner margin*/
  margin-left:1cm; /*outer margin*/
}

Weasyprint-margins.png

@bottom

Weasy-print also applies consistently @bottom rules, including page counting.

  @bottom-right {
    margin: 10pt 0 30pt 0;
    border-top: .25pt solid #FF05F6;
    content: "Testing WeasyPrint";
    font-size: 6pt;
    color: #00FFF2;
  }

  @bottom-center {
    margin: 10pt 0 30pt 0;
    content: counter(page);
    font-size: 6pt;
  }


CSS Custom Fonts

Weasy Print does not support CSS's (@font-face) rule.

To use custom fonts it relies on Pango library [4] for text layout.

A discussion on the use of custom fonts can be followed on Weasy Print's Github issue

Imposition

There appears to be no mechanism that performs page impositions.

Although it is possible to perform imposition over the Weasy Print-generated PDF with software such as BookletImposer, jPDF Tweak, pdftool, or PDFtk.


more tests

These tests and prototypes are far from exhaustive.

It is still unclear weather Weasy Print, CSS and HTML can produce quality PDFs, ready to send to a printer in order to produce paper-bound publication.

The results are promising.

Yet more experimentation, prototypes, and feedback is needed in order to make a informed critique.

And contributions to Weasy Print, will only help.


CSS style-sheet

@page {
size: A5 portrait;
}

@page:right {
  margin-left: 3cm; /*inner*/
  margin-right:1cm; /*outer*/ 
  
  @bottom-right {
    margin: 10pt 0 30pt 0;
    border-top: .25pt solid #FF05F6;
    content: "Testing WeasyPrint";
    font-size: 6pt;
    color: #00FFF2;
  }
  
  @bottom-center {
    margin: 10pt 0 30pt 0;
    content: counter(page);
    font-size: 6pt;
  }
}

@page:left {
  margin-right: 3cm; /*inner*/
  margin-left:1cm; /*outer*/

  @bottom-left {
    margin: 10pt 0 30pt 0;
    border-top: .25pt solid #00FFF2;
    content: "Testing WeasyPrint";
    font-size: 6pt;
    color: #FF05F6;
  }

  @bottom-center {
    margin: 10pt 0 30pt 0;
    content: counter(page);
    font-size: 6pt;
  }
}
 
@page:first {
  @bottom-center { content: ""; }
  @bottom-right { content: ""; }
}



body{
  font-size:8pt;
  font-family: sans;  
  padding: 0.5cm;
  }

a, a:visited, a:hover {color:#00FFF2;}

hr { color:#00FFF2;
   border:solid 1px #00FFF2;
}

div.title-beyond h1 {font-size:4em;
  margin-top:-40px;
} /*title*/

h1,h2,h3,h4,h5 {color:#FF05F6; }


figure {
  margin-left:0px;
  padding-top:0.5cm;  
}
  
img{ width: 6cm; }

figcaption { font-size:0.8em;
  width:6cm;
}


tutorials/posts on Weasy Print

http://diethardsteiner.github.io/reporting/2015/02/17/CSS-for-print.html

references

  1. See works by Lafkon studio for an idea of the possibilities
  2. “WeasyPrint Documentation” http://weasyprint.org/docs/.
  3. With the pyphen library
  4. http://www.pango.org/