Weasyprint: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "http://weasyprint.org/ ==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...")
 
 
(13 intermediate revisions by 2 users not shown)
Line 1: Line 1:
__TOC__
<blockquote>
"WeasyPrint is a visual rendering engine for HTML and CSS that can export to PDF. It aims to support web standards for printing. WeasyPrint is free software made available under a BSD license."
</blockquote>
http://weasyprint.org/
http://weasyprint.org/


==Testing Weasy Print==
Weasyprint can be used on the command line & as a Python library.
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.  
==Documentation==
 
* https://doc.courtbouillon.org/weasyprint/stable/
* https://doc.courtbouillon.org/weasyprint/stable/api_reference.html#weasyprint.__main__.main (CLI)
* https://doc.courtbouillon.org/weasyprint/stable/api_reference.html#python-api (Python library)
* https://doc.courtbouillon.org/weasyprint/stable/api_reference.html#supported-features (supported features)
 
==Testing WeasyPrint==
 
At the first glance WeasyPrint 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 WeasyPrint.  


The CSS style-sheet that gave origin the PDF is [[#CSS style-sheet|located at the end of this page]]
The CSS style-sheet that gave origin the PDF is [[#CSS style-sheet|located at the end of this page]]
Line 11: Line 28:


==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 20: Line 38:


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


Line 29: Line 48:


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


Line 63: Line 83:
</source>
</source>


== CSS Custom Fonts ==


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


Yet it can use fonts available in your system.  
Yet it can use fonts available in your system.  
Line 71: Line 91:
On Linux `fc-list` will give you a list of fonts installed in your system
On Linux `fc-list` will give you a list of fonts installed in your system


== Imposition ==
List system installed fonts:
There is no mechanism to performs page impositions.
   
$ fc-list  #show all the fonts available
/usr/share/fonts/type1/texlive-fonts-recommended/pcrbo8a.pfb: Courier:style=Bold Italic
/usr/share/fonts/type1/gsfonts/n019003l.pfb: Nimbus Sans L:style=Regular
/usr/share/fonts/truetype/UbuntuMono-B.ttf: Ubuntu Mono:style=Bold


However, 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].
Add that font - Ubuntu Mono - in stayle sheet


http://publicationstation.wdka.hro.nl/wiki/images/4/46/Web2print-BS07-booklet.pdf
<source lang="css">
Booklet produced with BookletImposer, having Weasy Print generated PDF as source
    body { font-size: 14pt;


          color: blue;


== More is needed ==
          font-family: "Ubuntu Mono";
These tests and prototypes are far from exhaustive.


It is still unclear whether Weasy Print, CSS and HTML can produce quality PDFs, ready to be sent to a printer in order to produce paper-bound publication.
        }
Yet the results seem promising.
</source>


More experimentation, prototypes, and feedback is needed in order to make a informed critique.
==Stylesheet example==
 
And contributions to [https://github.com/Kozea/WeasyPrint/ Weasy Print], can only help.
 
==CSS style-sheet==
<source lang="css">
<source lang="css">
@page {
@page {
Line 172: Line 192:
</source>
</source>


==See also==


==tutorials & posts on Weasy Print==
* [[Paged Media_CSS]]
http://diethardsteiner.github.io/reporting/2015/02/17/CSS-for-print.html
* [[Paged Media_CSS examples]]
 
* [[Paged.js]]
==references==
* [[Web-to-print]]
<references/>
 


[[Category:Cookbook]]
[[Category:Cookbook]]
[[Category:PagedMedia]]

Latest revision as of 11:02, 6 October 2023


"WeasyPrint is a visual rendering engine for HTML and CSS that can export to PDF. It aims to support web standards for printing. WeasyPrint is free software made available under a BSD license."

http://weasyprint.org/

Weasyprint can be used on the command line & as a Python library.

Documentation

Testing WeasyPrint

At the first glance WeasyPrint 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 WeasyPrint.

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

http://publicationstation.wdka.hro.nl/wiki/index.php/File: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

Option for the left and right pages, such as the margin sizes, which have to alternate in order to produce a bound work, are correctly rendered.

@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

WeasyPrint does not support CSS's @font-face rule.

Yet it can use fonts available in your system.

On Linux `fc-list` will give you a list of fonts installed in your system

List system installed fonts:

$ fc-list   #show all the fonts available
/usr/share/fonts/type1/texlive-fonts-recommended/pcrbo8a.pfb: Courier:style=Bold Italic
/usr/share/fonts/type1/gsfonts/n019003l.pfb: Nimbus Sans L:style=Regular
/usr/share/fonts/truetype/UbuntuMono-B.ttf: Ubuntu Mono:style=Bold

Add that font - Ubuntu Mono - in stayle sheet

    body { font-size: 14pt;

           color: blue;

           font-family: "Ubuntu Mono";

         }

Stylesheet example

@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;
}

See also