{"id":115642,"date":"2021-12-29T09:00:00","date_gmt":"2021-12-29T14:00:00","guid":{"rendered":"https:\/\/ibkrcampus.com\/?p=115642"},"modified":"2022-11-21T09:50:25","modified_gmt":"2022-11-21T14:50:25","slug":"how-to-create-pdf-files-with-python","status":"publish","type":"post","link":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-create-pdf-files-with-python\/","title":{"rendered":"How to Create PDF Files with Python"},"content":{"rendered":"\n<p>In a previous article we talked about several ways to&nbsp;<a href=\"https:\/\/theautomatic.net\/2020\/01\/21\/how-to-read-pdf-files-with-python\/\">read PDF files with Python<\/a>. This post will cover two packages used to&nbsp;<em>create<\/em>&nbsp;PDF files with Python, including&nbsp;<strong>pdfkit<\/strong>&nbsp;and&nbsp;<strong>ReportLab<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-create-pdf-files-with-python-and-pdfkit\"><strong>Create PDF files with Python and pdfkit<\/strong><\/h2>\n\n\n\n<p><strong>pdfkit<\/strong>&nbsp;was the first library I learned for creating PDF files. A nice feature of&nbsp;<strong>pdfkit<\/strong>&nbsp;is that you can use it to create PDF files from URLs. To get started, you\u2019ll need to install it along with a utility called&nbsp;<a href=\"https:\/\/wkhtmltopdf.org\/downloads.html\">wkhtmltopdf<\/a>. Use pip to install&nbsp;<strong>pdfkit<\/strong>&nbsp;from PyPI:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install pdfkit<\/code><\/pre>\n\n\n\n<p>Once you\u2019re set up, you can start using&nbsp;<strong>pdfkit<\/strong>. In the example below, we download Wikipedia\u2019s main page as a PDF file. To get&nbsp;<strong>pdfkit<\/strong>&nbsp;working, you\u2019ll need to either add&nbsp;<strong>wkhtmltopdf<\/strong>&nbsp;to your PATH, or configure&nbsp;<strong>pdfkit<\/strong>&nbsp;to point to where the executable is stored (the latter option is used below).<\/p>\n\n\n\n<p><strong>Download a webpage as a PDF<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># import package\nimport pdfkit\n \n# configure pdfkit to point to our installation of wkhtmltopdf\nconfig = pdfkit.configuration(wkhtmltopdf = r\"C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe\")\n \n# download Wikipedia main page as a PDF file\npdfkit.from_url(\"https:\/\/en.wikipedia.org\/wiki\/Main_Page\", \"sample_url_pdf.pdf\", configuration = config)\n<\/code><\/pre>\n\n\n\n<p>You can also set the output path to False, which will return a binary version of the PDF into Python, rather than downloading the webpage to an external file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pdfkit.from_url(\"https:\/\/en.wikipedia.org\/wiki\/Main_Page\", output_path = False, configuration = config)\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How to create a PDF from HTML<\/strong><\/h3>\n\n\n\n<p>One of the nicest features of&nbsp;<strong>pdfkit<\/strong>&nbsp;is that you can use it to create PDF files from HTML, including from HTML strings that you pass it directly in Python.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> = \"\"\"&lt;h1&gt;&lt;strong&gt;Sample PDF file from HTML&lt;\/strong&gt;&lt;\/h1&gt;\n       &lt;br&gt;&lt;\/br&gt;\n       &lt;p&gt;First line...&lt;\/p&gt;\n       &lt;p&gt;Second line...&lt;\/p&gt;\n       &lt;p&gt;Third line...&lt;\/p&gt;\"\"\"\n \npdfkit.from_string(s, output_path = \"new_file.pdf\", configuration = config)<\/code><\/pre>\n\n\n\n<p>Additionally,&nbsp;<strong>pdfkit<\/strong>&nbsp;can create PDF files by reading HTML files.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\npdfkit.from_file(\"sample_html_file.html\", output_path = \"new_file2.pdf\", configuration = config)\n<\/code><\/pre>\n\n\n\n<p>You can also create PDF files with more complex HTML \/ CSS, as well. You simply need to pass the HTML as a string or store it in a file that can be passed to&nbsp;<strong>pdfkit<\/strong>. Let\u2019s do another example, but this time, we\u2019ll create a table using HTML and CSS.<\/p>\n\n\n\n<p><strong>Creating tables in a PDF file<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>table_html = \"\"\"&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;style&gt;\ntable, th, td {\n  border: 1px solid black;\n}\n \ntable {\n  width: 100%;\n}\n&lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n \n&lt;h2&gt;Sample Table&lt;\/h2&gt;\n \n&lt;table&gt;\n  &lt;tr&gt;\n    &lt;th&gt;Field 1&lt;\/th&gt;\n    &lt;th&gt;Field 2&lt;\/th&gt;\n  &lt;\/tr&gt;\n  &lt;tr&gt;\n    &lt;td&gt;x1&lt;\/td&gt;\n    &lt;td&gt;x2&lt;\/td&gt;\n  &lt;\/tr&gt;\n  &lt;tr&gt;\n    &lt;td&gt;x3&lt;\/td&gt;\n    &lt;td&gt;x4&lt;\/td&gt;\n  &lt;\/tr&gt;\n&lt;\/table&gt;\n \n&lt;\/body&gt;\n&lt;\/html&gt;\n \"\"\"\n \npdfkit.from_string(table_html, output_path = \"sample_table.pdf\", configuration = config)\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"640\" height=\"111\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/create-pdf-table-with-python-theautomatic-net.png\" alt=\"\" class=\"wp-image-115946 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/create-pdf-table-with-python-theautomatic-net.png 640w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/create-pdf-table-with-python-theautomatic-net-300x52.png 300w\" data-sizes=\"(max-width: 640px) 100vw, 640px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 640px; aspect-ratio: 640\/111;\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Creating PDF files with Python and ReportLab<\/strong><\/h2>\n\n\n\n<p>The next package we\u2019ll discuss is&nbsp;<strong>ReportLab<\/strong>.&nbsp;<strong>ReportLab<\/strong>&nbsp;is one of the most popular libaries for creating PDF files.<\/p>\n\n\n\n<p>You can install&nbsp;<strong>ReportLab<\/strong>&nbsp;using pip:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install reportlab<\/code><\/pre>\n\n\n\n<p>Here\u2019s an initial example to create a simple PDF with one line of text. The first piece of code imports the&nbsp;<em>canvas<\/em>&nbsp;module from&nbsp;<strong>ReportLab<\/strong>. Then, we create an instance of the&nbsp;<em>Canvas<\/em>&nbsp;(note the capital \u201cC\u201d this time) class with the name of the file we want to create. Third, we use&nbsp;<em>drawString<\/em>&nbsp;to write out a line of text. The (50, 800) are coordinates for where to place the text (this might take some experimentation). Lastly, we save the file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from reportlab.pdfgen import canvas\n \nreport = canvas.Canvas(\"first_test.pdf\")\n \nreport.drawString(50, 800, \"**First PDF with ReportLab**\")\nreport.save()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Adding images to a PDF file<\/strong><\/h3>\n\n\n\n<p>Next, let\u2019s create a sample PDF file containing an image. Here, we\u2019re going to use the&nbsp;<strong>pillow<\/strong>&nbsp;library to create an&nbsp;<strong>Image<\/strong>&nbsp;object. In this example, we need to create a list of elements that we will use to construct the PDF file (we refer to this list as&nbsp;<em>info<\/em>&nbsp;below). For this instance, the list will contain just one element \u2013 the&nbsp;<strong>Image<\/strong>&nbsp;object represeting the image that we will put into the PDF file, but as we\u2019ll see in the next example, we can also use this list to store other elements for placing into the PDF file.<\/p>\n\n\n\n<p>Also, note here we are using the&nbsp;<strong>SimpleDocTemplate<\/strong>&nbsp;class, which basically does what it sounds like \u2013 creates a simple document template that we can use to fill in information. This provides more structure than using&nbsp;<strong>canvas<\/strong>, like above.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># import in SimpleDocTemplate\nfrom reportlab.platypus import SimpleDocTemplate\nfrom PIL import Image\n \n# create document object\ndoc = SimpleDocTemplate(\"sample_image.pdf\")\ninfo = &#91;]\n \n# directory to image file we want to use\nimage_file = \"sample_plot.png\"\n \n# create Image object with size specifications\nim = Image(image_file, 3*inch, 3*inch)\n \n# append Image object to our info list\ninfo.append(im)\n \n# build \/ save PDF document\ndoc.build(info)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Creating paragraphs of text<\/strong><\/h3>\n\n\n\n<p>Generalizing on our code above, we can add a few paragraphs of text, followed by a sample image.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from reportlab.platypus import Paragraph\n \ndoc = SimpleDocTemplate(\"more_text.pdf\")\n \np1 = \"&lt;font size = '12'&gt;&lt;strong&gt;This is the first paragraph...&lt;\/strong&gt;&lt;\/font&gt;\"\np2 = \"&lt;font size = '12'&gt;&lt;strong&gt;This is the second paragraph...&lt;\/strong&gt;&lt;\/font&gt;\"\np3 = \"&lt;font size = '12'&gt;&lt;strong&gt;This is the third paragraph...&lt;\/strong&gt;&lt;\/font&gt;\"\np4 = \"&lt;br&gt;&lt;\/br&gt;&lt;br&gt;&lt;\/br&gt;&lt;br&gt;&lt;\/br&gt;\"\n \nimage_file = \"sample_plot.png\"\n \nim = Image(image_file, 3*inch, 3*inch)\n \ninfo = &#91;]\n \ninfo.append(Paragraph(p1))\ninfo.append(Paragraph(p2))\ninfo.append(Paragraph(p3))\ninfo.append(Paragraph(p4))\ninfo.append(im)\n \ndoc.build(info)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How to adjust fonts<\/strong><\/h3>\n\n\n\n<p>To adjust font types, we can tweak our first&nbsp;<strong>ReportLab<\/strong>&nbsp;example above to use the&nbsp;<em>setFont<\/em>&nbsp;method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from reportlab.pdfgen import canvas\n \nreport = canvas.Canvas(\"test_with_font.pdf\")\n \nreport.setFont(\"Courier\", 12)\n \nreport.drawString(50, 800, \"**Test PDF with Different Font**\")\nreport.save()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Creating a PDF with multiple pages<\/strong><\/h3>\n\n\n\n<p>Next, let\u2019s show how to create a PDF with multiple pages. This is a common and useful task to be able to do. To handle creating multiple pages, we\u2019ll modify the above example to create a PDF with three separate pages. One way to tell&nbsp;<strong>ReportLab<\/strong>&nbsp;the content on a single page is finished is to use the&nbsp;<em>showPage<\/em>&nbsp;method, like below. Any content you create afterward will be added to the next page. Then, we can call the&nbsp;<em>showPage<\/em>&nbsp;method again to create a third page.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from reportlab.pdfgen import canvas\n \nreport = canvas.Canvas(\"multiple_pages.pdf\")\nreport.setFont(\"Courier\", 12)\n \nreport.drawString(50, 800, \"**This is the first page...**\")\nreport.showPage()\n \nreport.drawString(50, 800, \"**This is the second page...**\")\nreport.showPage()\n \nreport.drawString(50, 800, \"**This is the third page...**\")\nreport.showPage()\n \nreport.save()<\/code><\/pre>\n\n\n\n<p>Another way to create page breaks using the&nbsp;<strong>SimpleDocTemplate<\/strong>&nbsp;from earlier in the post is like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># import PageBreak, along with SimpleDocTemplate\nfrom reportlab.platypus import SimpleDocTemplate, PageBreak\n \n# create new file with image and multiple pages\ndoc = SimpleDocTemplate(\"sample_image_multiple_pages.pdf\")\ninfo = &#91;]\n \nimage_file = \"sample_plot.png\"\n \nim = Image(image_file, 3*inch, 3*inch)\ninfo.append(im)\n \n# add page break \ninfo.append(PageBreak())\ninfo.append(Paragraph(\"Second page...\"))\n \n# add third page\ninfo.append(PageBreak())\ninfo.append(Paragraph(\"Third page...\"))\n \n# build PDF\ndoc.build(info)<\/code><\/pre>\n\n\n\n<p><em>Visit <a href=\"https:\/\/theautomatic.net\/2021\/03\/25\/how-to-create-pdf-files-with-python\/\">TheAutomatic.net<\/a> to learn more about this topic.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post will cover two packages used to\u00a0create\u00a0PDF files with Python, including\u00a0pdfkit\u00a0and\u00a0ReportLab.<\/p>\n","protected":false},"author":388,"featured_media":19357,"comment_status":"closed","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[339,343,349,338,341,352,344],"tags":[10810,4804,595,10809],"contributors-categories":[13695],"class_list":{"0":"post-115642","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-science","8":"category-programing-languages","9":"category-python-development","10":"category-ibkr-quant-news","11":"category-quant-development","12":"category-quant-north-america","13":"category-quant-regions","14":"tag-pdfkit","15":"tag-pypi","16":"tag-python","17":"tag-reportlab","18":"contributors-categories-theautomatic-net"},"pp_statuses_selecting_workflow":false,"pp_workflow_action":"current","pp_status_selection":"publish","acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v27.5) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to Create PDF Files with Python | IBKR Quant<\/title>\n<meta name=\"description\" content=\"This post will cover two packages used to\u00a0create\u00a0PDF files with Python, including\u00a0pdfkit\u00a0and\u00a0ReportLab.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/115642\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Create PDF Files with Python | IBKR Quant Blog\" \/>\n<meta property=\"og:description\" content=\"This post will cover two packages used to\u00a0create\u00a0PDF files with Python, including\u00a0pdfkit\u00a0and\u00a0ReportLab.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-create-pdf-files-with-python\/\" \/>\n<meta property=\"og:site_name\" content=\"IBKR Campus US\" \/>\n<meta property=\"article:published_time\" content=\"2021-12-29T14:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-21T14:50:25+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2019\/09\/python-quant-blog.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1150\" \/>\n\t<meta property=\"og:image:height\" content=\"700\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Andrew Treadway\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Andrew Treadway\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t    \"@context\": \"https:\\\/\\\/schema.org\",\n\t    \"@graph\": [\n\t        {\n\t            \"@type\": \"NewsArticle\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-create-pdf-files-with-python\\\/#article\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-create-pdf-files-with-python\\\/\"\n\t            },\n\t            \"author\": {\n\t                \"name\": \"Andrew Treadway\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/d4018570a16fb867f1c08412fc9c64bc\"\n\t            },\n\t            \"headline\": \"How to Create PDF Files with Python\",\n\t            \"datePublished\": \"2021-12-29T14:00:00+00:00\",\n\t            \"dateModified\": \"2022-11-21T14:50:25+00:00\",\n\t            \"mainEntityOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-create-pdf-files-with-python\\\/\"\n\t            },\n\t            \"wordCount\": 774,\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-create-pdf-files-with-python\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2019\\\/09\\\/python-quant-blog.jpg\",\n\t            \"keywords\": [\n\t                \"pdfkit\",\n\t                \"PyPi\",\n\t                \"Python\",\n\t                \"ReportLab\"\n\t            ],\n\t            \"articleSection\": [\n\t                \"Data Science\",\n\t                \"Programming Languages\",\n\t                \"Python Development\",\n\t                \"Quant\",\n\t                \"Quant Development\",\n\t                \"Quant North America\",\n\t                \"Quant Regions\"\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"WebPage\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-create-pdf-files-with-python\\\/\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-create-pdf-files-with-python\\\/\",\n\t            \"name\": \"How to Create PDF Files with Python | IBKR Quant Blog\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\"\n\t            },\n\t            \"primaryImageOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-create-pdf-files-with-python\\\/#primaryimage\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-create-pdf-files-with-python\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2019\\\/09\\\/python-quant-blog.jpg\",\n\t            \"datePublished\": \"2021-12-29T14:00:00+00:00\",\n\t            \"dateModified\": \"2022-11-21T14:50:25+00:00\",\n\t            \"description\": \"This post will cover two packages used to\u00a0create\u00a0PDF files with Python, including\u00a0pdfkit\u00a0and\u00a0ReportLab.\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"ReadAction\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-create-pdf-files-with-python\\\/\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"ImageObject\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/how-to-create-pdf-files-with-python\\\/#primaryimage\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2019\\\/09\\\/python-quant-blog.jpg\",\n\t            \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2019\\\/09\\\/python-quant-blog.jpg\",\n\t            \"width\": 1150,\n\t            \"height\": 700,\n\t            \"caption\": \"Python Programming\"\n\t        },\n\t        {\n\t            \"@type\": \"WebSite\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"name\": \"IBKR Campus US\",\n\t            \"description\": \"Financial Education from Interactive Brokers\",\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"SearchAction\",\n\t                    \"target\": {\n\t                        \"@type\": \"EntryPoint\",\n\t                        \"urlTemplate\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/?s={search_term_string}\"\n\t                    },\n\t                    \"query-input\": {\n\t                        \"@type\": \"PropertyValueSpecification\",\n\t                        \"valueRequired\": true,\n\t                        \"valueName\": \"search_term_string\"\n\t                    }\n\t                }\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"Organization\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\",\n\t            \"name\": \"Interactive Brokers\",\n\t            \"alternateName\": \"IBKR\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"logo\": {\n\t                \"@type\": \"ImageObject\",\n\t                \"inLanguage\": \"en-US\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\",\n\t                \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"width\": 669,\n\t                \"height\": 669,\n\t                \"caption\": \"Interactive Brokers\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\"\n\t            },\n\t            \"publishingPrinciples\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/about-ibkr-campus\\\/\",\n\t            \"ethicsPolicy\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/cyber-security-notice\\\/\"\n\t        },\n\t        {\n\t            \"@type\": \"Person\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/d4018570a16fb867f1c08412fc9c64bc\",\n\t            \"name\": \"Andrew Treadway\",\n\t            \"description\": \"Andrew Treadway currently works as a Senior Data Scientist, and has experience doing analytics, software automation, and ETL. He completed a master\u2019s degree in computer science \\\/ machine learning, and an undergraduate degree in pure mathematics. Connect with him on LinkedIn: https:\\\/\\\/www.linkedin.com\\\/in\\\/andrew-treadway-a3b19b103\\\/In addition to TheAutomatic.net blog, he also teaches in-person courses on Python and R through my NYC meetup: more details.\",\n\t            \"sameAs\": [\n\t                \"https:\\\/\\\/theautomatic.net\\\/about-me\\\/\"\n\t            ],\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/author\\\/andrewtreadway\\\/\"\n\t        }\n\t    ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Create PDF Files with Python | IBKR Quant","description":"This post will cover two packages used to\u00a0create\u00a0PDF files with Python, including\u00a0pdfkit\u00a0and\u00a0ReportLab.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/115642\/","og_locale":"en_US","og_type":"article","og_title":"How to Create PDF Files with Python | IBKR Quant Blog","og_description":"This post will cover two packages used to\u00a0create\u00a0PDF files with Python, including\u00a0pdfkit\u00a0and\u00a0ReportLab.","og_url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-create-pdf-files-with-python\/","og_site_name":"IBKR Campus US","article_published_time":"2021-12-29T14:00:00+00:00","article_modified_time":"2022-11-21T14:50:25+00:00","og_image":[{"width":1150,"height":700,"url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2019\/09\/python-quant-blog.jpg","type":"image\/jpeg"}],"author":"Andrew Treadway","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Andrew Treadway","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-create-pdf-files-with-python\/#article","isPartOf":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-create-pdf-files-with-python\/"},"author":{"name":"Andrew Treadway","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/d4018570a16fb867f1c08412fc9c64bc"},"headline":"How to Create PDF Files with Python","datePublished":"2021-12-29T14:00:00+00:00","dateModified":"2022-11-21T14:50:25+00:00","mainEntityOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-create-pdf-files-with-python\/"},"wordCount":774,"publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-create-pdf-files-with-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2019\/09\/python-quant-blog.jpg","keywords":["pdfkit","PyPi","Python","ReportLab"],"articleSection":["Data Science","Programming Languages","Python Development","Quant","Quant Development","Quant North America","Quant Regions"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-create-pdf-files-with-python\/","url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-create-pdf-files-with-python\/","name":"How to Create PDF Files with Python | IBKR Quant Blog","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-create-pdf-files-with-python\/#primaryimage"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-create-pdf-files-with-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2019\/09\/python-quant-blog.jpg","datePublished":"2021-12-29T14:00:00+00:00","dateModified":"2022-11-21T14:50:25+00:00","description":"This post will cover two packages used to\u00a0create\u00a0PDF files with Python, including\u00a0pdfkit\u00a0and\u00a0ReportLab.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-create-pdf-files-with-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-create-pdf-files-with-python\/#primaryimage","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2019\/09\/python-quant-blog.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2019\/09\/python-quant-blog.jpg","width":1150,"height":700,"caption":"Python Programming"},{"@type":"WebSite","@id":"https:\/\/ibkrcampus.com\/campus\/#website","url":"https:\/\/ibkrcampus.com\/campus\/","name":"IBKR Campus US","description":"Financial Education from Interactive Brokers","publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ibkrcampus.com\/campus\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ibkrcampus.com\/campus\/#organization","name":"Interactive Brokers","alternateName":"IBKR","url":"https:\/\/ibkrcampus.com\/campus\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","width":669,"height":669,"caption":"Interactive Brokers"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/"},"publishingPrinciples":"https:\/\/www.interactivebrokers.com\/campus\/about-ibkr-campus\/","ethicsPolicy":"https:\/\/www.interactivebrokers.com\/campus\/cyber-security-notice\/"},{"@type":"Person","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/d4018570a16fb867f1c08412fc9c64bc","name":"Andrew Treadway","description":"Andrew Treadway currently works as a Senior Data Scientist, and has experience doing analytics, software automation, and ETL. He completed a master\u2019s degree in computer science \/ machine learning, and an undergraduate degree in pure mathematics. Connect with him on LinkedIn: https:\/\/www.linkedin.com\/in\/andrew-treadway-a3b19b103\/In addition to TheAutomatic.net blog, he also teaches in-person courses on Python and R through my NYC meetup: more details.","sameAs":["https:\/\/theautomatic.net\/about-me\/"],"url":"https:\/\/www.interactivebrokers.com\/campus\/author\/andrewtreadway\/"}]}},"jetpack_featured_media_url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2019\/09\/python-quant-blog.jpg","_links":{"self":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/115642","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/users\/388"}],"replies":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/comments?post=115642"}],"version-history":[{"count":0,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/115642\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media\/19357"}],"wp:attachment":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media?parent=115642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/categories?post=115642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/tags?post=115642"},{"taxonomy":"contributors-categories","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/contributors-categories?post=115642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}