{ "cells": [ { "cell_type": "markdown", "id": "08d488f2-e713-445d-b233-6795cfd779b2", "metadata": {}, "source": [ "# weasyprinting\n", "\n", "## How to use weasyprint to \"print\" to PDF...\n", "\n", "https://doc.courtbouillon.org/weasyprint/stable/" ] }, { "cell_type": "markdown", "id": "180230ba-471a-45f8-ab21-d6fe0b93f68c", "metadata": {}, "source": [ "### Using a URL (online)" ] }, { "cell_type": "code", "execution_count": 37, "id": "30279a2c-9635-46e8-a268-a0d0683b689b", "metadata": {}, "outputs": [], "source": [ "from weasyprint import HTML\n", "\n", "# The default is to give it a URL of something online\n", "HTML('http://weasyprint.org/').write_pdf('hello.pdf')" ] }, { "cell_type": "markdown", "id": "63862240-bc93-40ab-b370-a29e5af21244", "metadata": {}, "source": [ "### Using a (temp) file" ] }, { "cell_type": "code", "execution_count": 40, "id": "3d82141a-c101-4130-a466-3696fb022f5b", "metadata": {}, "outputs": [], "source": [ "from weasyprint import HTML, CSS\n", "\n", "# with is nice cause it auto-closes the file once \"outside\" the with block\n", "with open (\"hello.html\", \"w\") as output:\n", " print (\"
\", file=output)\n", " for i in range(40):\n", " print (\"x\"*i + \" \", file=output)\n", " print (\"\", file=output)\n", "HTML(filename=\"hello.html\").write_pdf('hello.pdf')" ] }, { "cell_type": "markdown", "id": "e110146f-cb30-497f-8914-4553bc3d5436", "metadata": {}, "source": [ "### Using a string\n", "\n", "The weasyprint HTML object can take a string of HTML source as input. Here += is used to add stuff to a single string variable (nb the first use of html is a regular assignment \"=\")." ] }, { "cell_type": "code", "execution_count": 39, "id": "d71b44de-3b65-434f-9107-2941db6ba861", "metadata": {}, "outputs": [], "source": [ "from weasyprint import HTML, CSS\n", "\n", "html = \"
\\n\"\n", "for i in range(40):\n", " html += \"x\"*i + \" \\n\"\n", "html += \"\"\n", "HTML(string=html).write_pdf('hello.pdf')" ] }, { "cell_type": "markdown", "id": "45811da9-ab1a-4475-a137-d443a0594f2a", "metadata": {}, "source": [ "### Using StringIO (a temporary file in memory)\n", "\n", "StringIO can be useful if you need (or prefer) to have a \"file object\" (like to use print), but don't want to actually make a file ... this is useful for instance when using in a web server where more than a script might be running more than once for different people and using a (fixed) filename would get you into trouble." ] }, { "cell_type": "code", "execution_count": 36, "id": "6b08cbae-60d1-4037-b9c9-59bd3a72a521", "metadata": {}, "outputs": [], "source": [ "from weasyprint import HTML, CSS\n", "import io\n", "\n", "# io.StringIO is like a temporary file\n", "output = io.StringIO()\n", "\n", "for i in range(40):\n", " print (\"x\"*i + \" \", file=output)\n", "\n", "# getvalue gets the contents of the \"file\" as a string\n", "html = output.getvalue()\n", "html = f\"
{html}\"\n", "HTML(string=html).write_pdf('hello.pdf',\n", " stylesheets=[CSS(string='body { font-family: monospace !important }')])" ] }, { "cell_type": "code", "execution_count": null, "id": "98a17a1b-0072-4743-b715-61e50bd4211c", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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": 5 }