{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Weasyprint\n", "\n", "Weasyprint is a python library to layout HTML (and CSS) as print pages, saving to a PDF. In this way, it can be a part of a \"web to print\" workflow." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "https://weasyprint.readthedocs.io/en/latest/tutorial.html" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from weasyprint import HTML, CSS\n", "from weasyprint.fonts import FontConfiguration" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## HTML()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The main class that weasyprint is HTML, it represents an HTML document, and provides functions to save as PDF (or PNG). When creating an HTML object you can specify the HTML either via HTML source as a string (via the *string* option), a file (via the *filename* option), or even an online page (via *url*)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "html = HTML(string='

hello

')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "or" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "html = HTML(filename=\"FILENAME.html\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "or" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "html = HTML(url=\"https://pzwiki.wdka.nl/mediadesign/Category:WordsfortheFuture\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## CSS()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The CSS class lets you include an (additional) CSS file. Just as with the HTML class, you can give a string, filename, or URL. If the HTML already has stylesheets, they will be combined. (is this true?)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "css = CSS(string='''\n", "@page{\n", " size: A4;\n", " margin: 15mm;\n", " background-color: lightgrey;\n", " font-family: monospace;\n", " font-size: 8pt;\n", " color: red;\n", " border:1px dotted red;\n", " \n", " @top-left{\n", " content: \"natural\";\n", " }\n", " @top-center{\n", " content: \"language\";\n", " }\n", " @top-right{\n", " content: \"artificial\";\n", " }\n", " @top-middle{\n", " content: \"\"\n", " }\n", " @left-top{\n", " content: \"computer control\";\n", " }\n", " @right-top{\n", " content: \"markup\";\n", " }\n", " @bottom-left{\n", " content: \"formal\";\n", " }\n", " @bottom-center{\n", " content: \"programming\";\n", " }\n", " @bottom-right{\n", " content: \"machine\";\n", " }\n", " }\n", " body{\n", " font-family: serif;\n", " font-size: 12pt;\n", " line-height: 1.4;\n", " color: magenta;\n", " }\n", " h1{\n", " width: 100%;\n", " text-align: center;\n", " font-size: 250%;\n", " line-height: 1.25;\n", " color: orange;\n", " }\n", " strong{\n", " color: blue;\n", " }\n", " em{\n", " color: green;\n", " }\n", "\n", "''')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "html.write_pdf('mydocument.pdf', stylesheets=[css])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Preview your PDF in the notebook!\n", "from IPython.display import IFrame, display\n", "IFrame(\"mydocument.pdf\", width=900, height=600)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 4 }