You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

463 lines
34 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Reportlab Cheatsheet"
]
},
{
"cell_type": "code",
"execution_count": null,
"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/8Vx08OtSsg29dNDERxbfWcPBXOiwenbExvVMya0bBwQU12l1F7x39OXyXU7Suj9t39OXyV+/hcHumc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rnt1iXarC4AXN9iCi4w3X
"\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. \n",
"\n",
"*tip*: the Reportlab Userguide, https://www.reportlab.com/docs/reportlab-userguide.pdf"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Canvas"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"Canvas?"
]
},
{
"cell_type": "code",
"execution_count": null,
"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": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Add a page\n",
"c.showPage()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Save the PDF\n",
"c.save()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Shapes"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"c.circle?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"c.circle(20*mm, 20*mm, 10*mm, stroke=0, fill=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"c.rect?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"c.rect(40*mm, 40*mm, 10*mm, 10*mm, stroke=1, fill=0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"c.roundRect?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"c.roundRect(60*mm, 60*mm, 50*mm, 50*mm, 5*mm, stroke=1, fill=0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"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": null,
"metadata": {},
"outputs": [],
"source": [
"# Reportlab comes with a set of fonts\n",
"c.getAvailableFonts()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"c.setFont('Courier', 72)"
]
},
{
"cell_type": "code",
"execution_count": null,
"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": null,
"metadata": {},
"outputs": [],
"source": [
"c.setFont('OSP-DIN', 72)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"c.drawCentredString(105*mm, 50*mm, \"Hello :)!\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"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": null,
"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": null,
"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": null,
"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": null,
"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": "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
}