diff --git a/ASCII-canvas-to-PDF.ipynb b/ASCII-canvas-to-PDF.ipynb new file mode 100644 index 0000000..53eae10 --- /dev/null +++ b/ASCII-canvas-to-PDF.ipynb @@ -0,0 +1,275 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# ASCII Canvas" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "(using Reportlab to export it as a PDF)" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": {}, + "outputs": [], + "source": [ + "width = 75\n", + "height = 65" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "metadata": {}, + "outputs": [], + "source": [ + "multipage = False" + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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xxxxxxxxxxxx\n", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n", + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" + ] + } + ], + "source": [ + "# Simple fill-up of a page, to set the reach of our canvas\n", + "lines = [] # All the lines will be stored here\n", + "\n", + "for linenumber in range(height):\n", + " line = 'x' * width\n", + " lines.append(line)\n", + "\n", + "print('\\n'.join(lines))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "# Another variation\n", + "import random \n", + "\n", + "lines = []\n", + "\n", + "txt = open('txt/language.txt', 'r').read()\n", + "words = txt.split()\n", + "\n", + "for linenumber in range(height):\n", + " word = random.choice(words)\n", + " length_of_word = len(word)\n", + " nr_of_words_fit_in_line = width / length_of_word\n", + " line = word * int(nr_of_words_fit_in_line)\n", + " lines.append(line)\n", + " \n", + "print('\\n'.join(lines))" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "metadata": {}, + "outputs": [], + "source": [ + "# Same variation, but with multiple pages\n", + "import random \n", + "\n", + "multipage = True\n", + "\n", + "lines = []\n", + "pages = []\n", + "\n", + "txt = open('txt/language.txt', 'r').read()\n", + "words = txt.split()\n", + "\n", + "for page in range(100):\n", + " \n", + " for linenumber in range(height):\n", + " word = random.choice(words)\n", + " length_of_word = len(word)\n", + " nr_of_words_fit_in_line = width / length_of_word\n", + " line = word * int(nr_of_words_fit_in_line)\n", + " lines.append(line)\n", + " \n", + " # Add a page\n", + " pages.append(lines)\n", + " lines = []\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exporting with Reportlab" + ] + }, + { + "cell_type": "code", + "execution_count": 91, + "metadata": {}, + "outputs": [], + "source": [ + "from reportlab.pdfgen.canvas import Canvas\n", + "from reportlab.lib.pagesizes import A4\n", + "from reportlab.lib.units import mm\n", + "\n", + "pagewidth = 210*mm\n", + "pageheight = 297*mm\n", + "\n", + "c = Canvas(\"pdf/ASCII-canvas-to-PDF.pdf\", pagesize=(pagewidth, pageheight), bottomup=0)\n", + "c.setFont('Courier', 12)\n", + "\n", + "start_y = 10*mm # start position of the lines\n", + "\n", + "y = start_y\n", + "lineheight = 4*mm\n", + "\n", + "if multipage == True:\n", + " for page in pages:\n", + " for line in page:\n", + " c.drawCentredString(pagewidth/2, y, line)\n", + " y += lineheight\n", + " c.showPage()\n", + " y = start_y\n", + "else:\n", + " for line in lines:\n", + " c.drawCentredString(pagewidth/2, y, line)\n", + " y += lineheight\n", + " \n", + "c.save()" + ] + }, + { + "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 +} diff --git a/reportlab-canvas-A4-bag-of-words.ipynb b/reportlab-canvas-A4-bag-of-words.ipynb new file mode 100644 index 0000000..fabee4a --- /dev/null +++ b/reportlab-canvas-A4-bag-of-words.ipynb @@ -0,0 +1,297 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 266, + "metadata": {}, + "outputs": [], + "source": [ + "from reportlab.pdfgen.canvas import Canvas\n", + "from reportlab.lib.pagesizes import A4\n", + "from reportlab.lib.units import mm\n", + "from reportlab.lib.colors import pink, magenta, red, blue, yellow, lightyellow" + ] + }, + { + "cell_type": "code", + "execution_count": 277, + "metadata": {}, + "outputs": [], + "source": [ + "c = Canvas(\"pdf/reportlab-canvas-A4-bag-of-words.pdf\", pagesize=(210*mm, 297*mm), bottomup=0)" + ] + }, + { + "cell_type": "code", + "execution_count": 268, + "metadata": {}, + "outputs": [], + "source": [ + "c.setPageSize(A4)" + ] + }, + { + "cell_type": "code", + "execution_count": 269, + "metadata": {}, + "outputs": [], + "source": [ + "# Draw a background color\n", + "c.setFillColor(lightyellow)\n", + "c.rect(0, 0, A4[0], A4[1], stroke=0, fill=1)" + ] + }, + { + "cell_type": "code", + "execution_count": 270, + "metadata": {}, + "outputs": [], + "source": [ + "# Draw a bag-of-words on the canvas, on random positions\n", + "import random\n", + "\n", + "txt = open('txt/language.txt', 'r').read()\n", + "words = txt.split()" + ] + }, + { + "cell_type": "code", + "execution_count": 271, + "metadata": {}, + "outputs": [], + "source": [ + "# Set a font, change the font color\n", + "from reportlab.pdfbase.ttfonts import TTFont, pdfmetrics\n", + "\n", + "fontpath = \"fonts/OSP-DIN.ttf\"\n", + "font = TTFont('OSP-DIN', fontpath)\n", + "pdfmetrics.registerFont(font)\n", + "\n", + "c.setFont('OSP-DIN', 32)\n", + "c.setFillColor(magenta)" + ] + }, + { + "cell_type": "code", + "execution_count": 275, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "37 107 a\n", + "24 100 formal\n", + "126 264 a\n", + "58 279 controls\n", + "41 250 an\n", + "36 142 program\n", + "111 153 programming\n", + "32 228 be\n", + "71 166 Originally\n", + "154 267 operations)\n", + "97 60 are\n", + "187 158 the\n", + "98 205 an\n", + "169 229 “code”\n", + "175 144 analog\n", + "166 9 yet\n", + "82 182 the\n", + "136 34 a\n", + "35 268 7\n", + "13 21 of\n", + "152 254 of\n", + "81 157 call\n", + "160 104 tomorrow\n", + "141 47 promises\n", + "140 220 of\n", + "155 201 The\n", + "62 59 terms.”\n", + "41 246 difficult\n", + "61 139 of\n", + "154 279 in\n", + "48 279 coded\n", + "59 88 artists\n", + "146 186 eventual\n", + "90 26 virus\n", + "102 261 act\n", + "55 265 computer\n", + "83 40 reflect\n", + "126 144 experiments\n", + "99 30 computation\n", + "5 221 concrete\n", + "96 129 occurs\n", + "19 89 computes\n", + "181 34 their\n", + "9 57 is\n", + "157 68 “performative”\n", + "19 95 computer\n", + "30 44 in\n", + "88 114 instructions\n", + "65 188 computer\n", + "81 149 German\n", + "70 207 whatever\n", + "113 31 representation\n", + "166 82 and\n", + "15 55 to\n", + "57 163 and\n", + "118 196 is\n", + "51 223 as\n", + "67 46 a\n", + "38 278 handle\n", + "136 134 Yet\n", + "35 170 poetry\n", + "146 111 “Interface”)\n", + "158 132 suitable\n", + "187 189 example\n", + "159 200 would\n", + "33 241 that\n", + "178 213 nothing\n", + "62 78 critically\n", + "166 205 of\n", + "169 42 more\n", + "122 154 itself\n", + "187 278 formal\n", + "85 59 ”Chapter\n", + "35 104 themselves\n", + "145 64 states\n", + "37 85 or\n", + "81 226 that\n", + "19 253 \n", + "157 55 string\n", + "187 125 semantic\n", + "142 224 transformation\n", + "115 3 the\n", + "98 46 Wesley\n", + "61 202 a\n", + "178 32 in\n", + "110 211 command\n", + "169 240 be\n", + "166 82 symbols\n", + "34 192 languages\n", + "30 68 is\n", + "46 172 that\n", + "3 276 “natural\n", + "123 242 computer\n", + "100 123 common\n", + "162 248 that\n", + "140 31 it\n", + "9 146 are\n", + "165 10 in\n", + "132 271 control\n", + "173 276 But\n" + ] + } + ], + "source": [ + "# Draw on the canvas\n", + "for n in range(100):\n", + " x = random.randint(0, 190)\n", + " y = random.randint(0, 280)\n", + " word = random.choice(words).strip(',.')\n", + " print(x, y, word)\n", + " c.drawString(x*mm, y*mm, word)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 273, + "metadata": {}, + "outputs": [], + "source": [ + "# Add a small caption\n", + "c.setFont('Courier', 10)\n", + "c.setFillColor(blue)\n", + "c.drawCentredString(105*mm, 285*mm, 'Random words from Language and Software Studies, by Florian Cramer (2005)')" + ] + }, + { + "cell_type": "code", + "execution_count": 274, + "metadata": {}, + "outputs": [], + "source": [ + "# Save the PDF!\n", + "#c.showPage()\n", + "c.save()" + ] + }, + { + "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 +} diff --git a/reportlab-cheatsheet.ipynb b/reportlab-cheatsheet.ipynb new file mode 100644 index 0000000..ed91edf --- /dev/null +++ b/reportlab-cheatsheet.ipynb @@ -0,0 +1,617 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Reportlab Cheatsheet" + ] + }, + { + "cell_type": "code", + "execution_count": 110, + "metadata": {}, + "outputs": [], + "source": [ + "from reportlab.pdfgen.canvas import Canvas\n", + "from reportlab.lib.pagesizes import letter, A4\n", + "from reportlab.lib.units import inch, mm" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# ReportLab PDF Library\n", + "\n", + "![](data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxATDxAQEhMVFhIXFhAQGBIVFhAVFhUWFRYWFhUVFRYYHSggGBolHRUTITEiJSkrLi4uFx8zODMsNygtLisBCgoKBQUFDgUFDisZExkrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrK//AABEIALMBGQMBIgACEQEDEQH/xAAcAAEAAwADAQEAAAAAAAAAAAAABQYHAgMEAQj/xABEEAABAwICBgYGCAQFBQEAAAABAAIDBBEFIQYSMUFRkQcTYXGh0SIyUlOBkhQWF0JUYrHBFSOT4UOCwtLwM3KisuJj/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/ANxREQEREBERAREQEREBEXTUzaoFkHcijXVr+zkus4g/s5IJZFDOxOTs5Lrdi0n5eX90E6irzsal/LyPmut2OTfl5HzQWVFVH6QzjczkfNdLtJ5/ycj5oLiipDtKqj/8+TvNdL9L6obo/ld/uQX1Fnb9Nasbovld/uXvwHTR0kgjma0X2ObcA/AnJBdUXwFfUBERAREQEREBERAREQEREBERAREQEREBERAUfiL8wOxSCh6593nkg8OJV0cMT5pXasbBrOdwHw71BR6aUDmhzZSQcwerlz/8Vx08OtSsg29dNDERxbfWcPBXOiwenbExvVMya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ERAREQEREBERAREQEREGadLdZ/MpouAfIR4BZ5VT2jeew+OSn+kqu18SlG5jWR/G1z+oVXkAcAL5XaT2gG9kG66AUXVYdA38oPNWJZpRdJcbI2RimdZoDfXG74L0DpQb+Gd/Ub5INDRZ79pzfw5+ceSfaez8OfnHkg0JFn32nN/Du+ceSfaa38OfnHkg0FFn/wBpjfwzvnHkvn2mt/DO+ceSDQUWffac38M75x5J9pzfwzvnHkg0FZb0rVN6qGPc2Mut2vd/8qQ+09v4c/OPJUjSXFvpVS+fV1QQ0BpN7Bo499+aCHryerdbaRqjvOS3jRCk6qhp2cGBYXFFrzU8Q+/KwfAG/wCy/RFNHqsY3gAPBB2oiICIiAiIgIiICIiAiIgIiICIiAiIgL4SvqIPzXpBV69bUvORMsm3I2BsMj2BeVs44jmFvVVoRQyPdI+PWc43JO0rq+oGHe6HggxBs49ocwuYnb7Q5hbZ9QMO90PBPqBh3uh4IMVE49ocwuX0ge0ObVtH1Aw73Q8E+oGHe6HggxgTj2hzb5rmKhvtDm1bJ9QMO90PBPs/w73I8EGOfSG+03mFx+kN9pvMLZfs/wAO9yPBPs/w73I8EGMmZvEcwnXt9ofMFs32f4d7keCfZ/h3uR4IMYMzfaHzNXzrWcRzHmto+oGHe6Hgn1Aw73Q8EGX6D0/XYrTgZtYHPJGYG4X8VvKi8F0fpqXW6mMNLtp3qUQEREBERAREQEREBERAREQEREBERAREQEREBERAREQEREBERAREQEREBERAREQEREBERAREQEREBERAREQf/9k=)\n", + "\n", + "What is the ReportLab PDF Library?\n", + "\n", + "This is a software library that lets you directly create documents in Adobe's Portable Document Format (PDF)using the Python programming language. It also creates charts and data graphics in various bitmap and vectorformats as well as PDF.\n", + "\n", + "https://www.reportlab.com/docs/reportlab-userguide.pdf" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Canvas" + ] + }, + { + "cell_type": "code", + "execution_count": 111, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "\u001b[0;31mInit signature:\u001b[0m\n", + "\u001b[0mCanvas\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n", + "\u001b[0;34m\u001b[0m \u001b[0mfilename\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", + "\u001b[0;34m\u001b[0m \u001b[0mpagesize\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", + "\u001b[0;34m\u001b[0m \u001b[0mbottomup\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", + "\u001b[0;34m\u001b[0m \u001b[0mpageCompression\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", + "\u001b[0;34m\u001b[0m \u001b[0minvariant\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", + "\u001b[0;34m\u001b[0m \u001b[0mverbosity\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", + "\u001b[0;34m\u001b[0m \u001b[0mencrypt\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", + "\u001b[0;34m\u001b[0m \u001b[0mcropMarks\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", + "\u001b[0;34m\u001b[0m \u001b[0mpdfVersion\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", + "\u001b[0;34m\u001b[0m \u001b[0menforceColorSpace\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", + "\u001b[0;34m\u001b[0m \u001b[0minitialFontName\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", + "\u001b[0;34m\u001b[0m \u001b[0minitialFontSize\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", + "\u001b[0;34m\u001b[0m \u001b[0minitialLeading\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", + "\u001b[0;34m\u001b[0m \u001b[0mcropBox\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", + "\u001b[0;34m\u001b[0m \u001b[0martBox\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", + "\u001b[0;34m\u001b[0m \u001b[0mtrimBox\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", + "\u001b[0;34m\u001b[0m \u001b[0mbleedBox\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", + "\u001b[0;34m\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mDocstring:\u001b[0m \n", + "This class is the programmer's interface to the PDF file format. Methods\n", + "are (or will be) provided here to do just about everything PDF can do.\n", + "\n", + "The underlying model to the canvas concept is that of a graphics state machine\n", + "that at any given point in time has a current font, fill color (for figure\n", + "interiors), stroke color (for figure borders), line width and geometric transform, among\n", + "many other characteristics.\n", + "\n", + "Canvas methods generally either draw something (like canvas.line) using the\n", + "current state of the canvas or change some component of the canvas\n", + "state (like canvas.setFont). The current state can be saved and restored\n", + "using the saveState/restoreState methods.\n", + "\n", + "Objects are \"painted\" in the order they are drawn so if, for example\n", + "two rectangles overlap the last draw will appear \"on top\". PDF form\n", + "objects (supported here) are used to draw complex drawings only once,\n", + "for possible repeated use.\n", + "\n", + "There are other features of canvas which are not visible when printed,\n", + "such as outlines and bookmarks which are used for navigating a document\n", + "in a viewer.\n", + "\n", + "Here is a very silly example usage which generates a Hello World pdf document.\n", + "\n", + "Example:: \n", + "\n", + " from reportlab.pdfgen import canvas\n", + " c = canvas.Canvas(\"hello.pdf\")\n", + " from reportlab.lib.units import inch\n", + " # move the origin up and to the left\n", + " c.translate(inch,inch)\n", + " # define a large font\n", + " c.setFont(\"Helvetica\", 80)\n", + " # choose some colors\n", + " c.setStrokeColorRGB(0.2,0.5,0.3)\n", + " c.setFillColorRGB(1,0,1)\n", + " # draw a rectangle\n", + " c.rect(inch,inch,6*inch,9*inch, fill=1)\n", + " # make text go straight up\n", + " c.rotate(90)\n", + " # change color\n", + " c.setFillColorRGB(0,0,0.77)\n", + " # say hello (note after rotate the y coord needs to be negative!)\n", + " c.drawString(3*inch, -3*inch, \"Hello World\")\n", + " c.showPage()\n", + " c.save()\n", + "\u001b[0;31mInit docstring:\u001b[0m\n", + "Create a canvas of a given size. etc.\n", + "\n", + "You may pass a file-like object to filename as an alternative to\n", + "a string.\n", + "For more information about the encrypt parameter refer to the setEncrypt method.\n", + "\n", + "Most of the attributes are private - we will use set/get methods\n", + "as the preferred interface. Default page size is A4.\n", + "cropMarks may be True/False or an object with parameters borderWidth, markColor, markWidth\n", + "and markLength\n", + "\n", + "if enforceColorSpace is in ('cmyk', 'rgb', 'sep','sep_black','sep_cmyk') then one of\n", + "the standard _PDFColorSetter callables will be used to enforce appropriate color settings.\n", + "If it is a callable then that will be used.\n", + "\u001b[0;31mFile:\u001b[0m ~/.local/lib/python3.7/site-packages/reportlab/pdfgen/canvas.py\n", + "\u001b[0;31mType:\u001b[0m type\n", + "\u001b[0;31mSubclasses:\u001b[0m \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "Canvas?" + ] + }, + { + "cell_type": "code", + "execution_count": 112, + "metadata": {}, + "outputs": [], + "source": [ + "# Make a Canvas object, bottomup=0 will put the 0,0 at the lop-left (instead of bottom-left)\n", + "c = Canvas(\"pdf/reportlab-cheatsheet.pdf\", pagesize=A4, bottomup=0)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Shapes" + ] + }, + { + "cell_type": "code", + "execution_count": 113, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\u001b[0;31mSignature:\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcircle\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx_cen\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my_cen\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstroke\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfill\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mDocstring:\u001b[0m draw a cirle centered at (x_cen,y_cen) with radius r (special case of ellipse)\n", + "\u001b[0;31mFile:\u001b[0m ~/.local/lib/python3.7/site-packages/reportlab/pdfgen/canvas.py\n", + "\u001b[0;31mType:\u001b[0m method\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "c.circle?" + ] + }, + { + "cell_type": "code", + "execution_count": 114, + "metadata": {}, + "outputs": [], + "source": [ + "c.circle(20*mm, 20*mm, 10*mm, stroke=0, fill=1)" + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\u001b[0;31mSignature:\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrect\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mwidth\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mheight\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstroke\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfill\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mDocstring:\u001b[0m draws a rectangle with lower left corner at (x,y) and width and height as given.\n", + "\u001b[0;31mFile:\u001b[0m ~/.local/lib/python3.7/site-packages/reportlab/pdfgen/canvas.py\n", + "\u001b[0;31mType:\u001b[0m method\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "c.rect?" + ] + }, + { + "cell_type": "code", + "execution_count": 116, + "metadata": {}, + "outputs": [], + "source": [ + "c.rect(40*mm, 40*mm, 10*mm, 10*mm, stroke=1, fill=0)" + ] + }, + { + "cell_type": "code", + "execution_count": 117, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\u001b[0;31mSignature:\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mroundRect\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mwidth\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mheight\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mradius\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstroke\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfill\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mDocstring:\u001b[0m\n", + "Draws a rectangle with rounded corners. The corners are\n", + "approximately quadrants of a circle, with the given radius.\n", + "\u001b[0;31mFile:\u001b[0m ~/.local/lib/python3.7/site-packages/reportlab/pdfgen/canvas.py\n", + "\u001b[0;31mType:\u001b[0m method\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "c.roundRect?" + ] + }, + { + "cell_type": "code", + "execution_count": 118, + "metadata": {}, + "outputs": [], + "source": [ + "c.roundRect(60*mm, 60*mm, 50*mm, 50*mm, 5*mm, stroke=1, fill=0)" + ] + }, + { + "cell_type": "code", + "execution_count": 119, + "metadata": {}, + "outputs": [], + "source": [ + "c.showPage()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Fonts" + ] + }, + { + "cell_type": "code", + "execution_count": 120, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Courier',\n", + " 'Courier-Bold',\n", + " 'Courier-BoldOblique',\n", + " 'Courier-Oblique',\n", + " 'Helvetica',\n", + " 'Helvetica-Bold',\n", + " 'Helvetica-BoldOblique',\n", + " 'Helvetica-Oblique',\n", + " 'Symbol',\n", + " 'Times-Bold',\n", + " 'Times-BoldItalic',\n", + " 'Times-Italic',\n", + " 'Times-Roman',\n", + " 'ZapfDingbats']" + ] + }, + "execution_count": 120, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Reportlab comes with a set of fonts\n", + "c.getAvailableFonts()" + ] + }, + { + "cell_type": "code", + "execution_count": 131, + "metadata": {}, + "outputs": [], + "source": [ + "c.setFont('Courier', 72)" + ] + }, + { + "cell_type": "code", + "execution_count": 121, + "metadata": {}, + "outputs": [], + "source": [ + "# Or you can import fonts\n", + "from reportlab.pdfbase.ttfonts import TTFont, pdfmetrics\n", + "\n", + "fontpath = \"fonts/OSP-DIN.ttf\"\n", + "font = TTFont('OSP-DIN', fontpath)\n", + "pdfmetrics.registerFont(font)" + ] + }, + { + "cell_type": "code", + "execution_count": 122, + "metadata": {}, + "outputs": [], + "source": [ + "c.setFont('OSP-DIN', 72)" + ] + }, + { + "cell_type": "code", + "execution_count": 123, + "metadata": {}, + "outputs": [], + "source": [ + "c.drawCentredString(105*mm, 50*mm, \"Hello :)!\")" + ] + }, + { + "cell_type": "code", + "execution_count": 124, + "metadata": {}, + "outputs": [], + "source": [ + "c.showPage()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Colors" + ] + }, + { + "cell_type": "code", + "execution_count": 126, + "metadata": {}, + "outputs": [], + "source": [ + "# Choose a RGB/CMYK color\n", + "\n", + "# Fill-color\n", + "c.setFillColorRGB(255,0,0)\n", + "c.setFillColorCMYK(0,1,0,0)\n", + "\n", + "# Stroke-color\n", + "c.setStrokeColorRGB(255,255,0)\n", + "c.setStrokeColorCMYK(0,0,1,0)" + ] + }, + { + "cell_type": "code", + "execution_count": 134, + "metadata": {}, + "outputs": [], + "source": [ + "# Or choose one of the embedded colors from Reportlab\n", + "from reportlab.lib.colors import pink, magenta, red, blue\n", + "\n", + "c.setFillColor(magenta)\n", + "c.setStrokeColor(red)" + ] + }, + { + "cell_type": "code", + "execution_count": 128, + "metadata": {}, + "outputs": [], + "source": [ + "c.setFont('OSP-DIN', 72)\n", + "c.drawCentredString(105*mm, 50*mm, \"Hello :)!\")\n", + "\n", + "c.rect(105*mm, 150*mm, 50*mm, 50*mm, stroke=1, fill=1)" + ] + }, + { + "cell_type": "code", + "execution_count": 129, + "metadata": {}, + "outputs": [], + "source": [ + "c.showPage()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Lines" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Text" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Images" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Save PDF" + ] + }, + { + "cell_type": "code", + "execution_count": 130, + "metadata": {}, + "outputs": [], + "source": [ + "c.save()" + ] + }, + { + "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 +}