{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Markdown - HTML - print" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pypandoc\n", "from weasyprint import HTML, CSS\n", "from weasyprint.fonts import FontConfiguration" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Markdown → HTML" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pandoc: \"If you need to convert files from one markup format into another, **pandoc is your swiss-army knife**.\"\n", "\n", "https://pandoc.org/\n", "\n", "The Python library for Pandoc:\n", "\n", "https://github.com/bebraw/pypandoc \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Convert a Markdown file to HTML ...\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# ... directly from a file\n", "html = pypandoc.convert_file('language.md', 'html')\n", "print(html)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# ... or from a pad\n", "\n", "from urllib.request import urlopen\n", "\n", "url = 'https://pad.xpub.nl/p/language/export/txt'\n", "response = urlopen(url)\n", "md = response.read().decode('UTF-8')\n", "\n", "with open('language.md', 'w') as f:\n", " f.write(md)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "html = pypandoc.convert_file('language.md', 'html')\n", "print(html)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## HTML → PDF" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "for this we can use Weasyprint again" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "html = HTML(string=html)\n", "print(html)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "css = CSS(string='''\n", "@page{\n", " size: A4;\n", " margin: 15mm;\n", " \n", " counter-increment: page;\n", " \n", " @top-left{\n", " content: \"hello?\";\n", " }\n", " @top-center{\n", " content: counter(page);\n", " font-size: 7pt;\n", " font-family: monospace;\n", " color: blue;\n", " }\n", " @bottom-center{\n", " content: \"this is the bottom center!\";\n", " }\n", " }\n", " \n", " body{\n", " color: magenta;\n", " }\n", "''')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It's actually interesting and useful to have a close look at paged media properties in CSS: \n", "\n", "https://developer.mozilla.org/en-US/docs/Web/CSS/%40page/size" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "html.write_pdf('language.pdf', stylesheets=[css])" ] }, { "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 }