{"title":"Tworzenie PDF’ów w Django","version":"3.2","locale":"pl","docname":"howto/outputting-pdf","url":"/pl/3.2/howto/outputting-pdf/","canonical":"https://djangodocs.dev/pl/3.2/howto/outputting-pdf/","summary":"This document explains how to output PDF files dynamically using Django views. This is made possible by the excellent, open-source ReportLab Python PDF library. The…","html":"<h1>Tworzenie PDF’ów w Django<a class=\"heading-anchor\" href=\"#outputting-pdfs-with-django\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>This document explains how to output PDF files dynamically using Django views.\nThis is made possible by the excellent, open-source <a class=\"reference external\" href=\"https://www.reportlab.com/opensource/\">ReportLab</a> Python PDF\nlibrary.</p>\n<p>The advantage of generating PDF files dynamically is that you can create\ncustomized PDFs for different purposes – say, for different users or different\npieces of content.</p>\n<p>For example, Django was used at <a class=\"reference external\" href=\"http://www2.kusports.com/\">kusports.com</a> to generate customized,\nprinter-friendly NCAA tournament brackets, as PDF files, for people\nparticipating in a March Madness contest.</p>\n<section id=\"install-reportlab\">\n<h2>Zainstaluj ReportLab<a class=\"heading-anchor\" href=\"#install-reportlab\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The ReportLab library is <a class=\"reference external\" href=\"https://pypi.org/project/reportlab/\">available on PyPI</a>. A <a class=\"reference external\" href=\"https://www.reportlab.com/docs/reportlab-userguide.pdf\">user guide</a> (not\ncoincidentally, a PDF file) is also available for download.\nYou can install ReportLab with <code class=\"docutils literal notranslate\"><span class=\"pre\">pip</span></code>:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-0-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>python<span class=\"w\"> </span>-m<span class=\"w\"> </span>pip<span class=\"w\"> </span>install<span class=\"w\"> </span>reportlab\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-0-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> py -m pip install reportlab\n</code></pre></div></div></div>\n<p>Test your installation by importing it in the Python interactive interpreter:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">import</span><span class=\"w\"> </span><span class=\"nn\">reportlab</span>\n</code></pre></div>\n<p>Jeśli to polecenie nie pokazało żadnego błędu, instalacja przebiegła pomyślnie.</p>\n</section>\n<section id=\"write-your-view\">\n<h2>Napisz swój widok<a class=\"heading-anchor\" href=\"#write-your-view\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The key to generating PDFs dynamically with Django is that the ReportLab API\nacts on file-like objects, and Django’s <a class=\"reference internal\" href=\"/pl/3.2/ref/request-response/#django.http.FileResponse\" title=\"django.http.FileResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">FileResponse</span></code></a>\nobjects accept file-like objects.</p>\n<p>Oto przykład „Hello World”:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">import</span><span class=\"w\"> </span><span class=\"nn\">io</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.http</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">FileResponse</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">reportlab.pdfgen</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">canvas</span>\n\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">some_view</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">):</span>\n    <span class=\"c1\"># Create a file-like buffer to receive PDF data.</span>\n    <span class=\"n\">buffer</span> <span class=\"o\">=</span> <span class=\"n\">io</span><span class=\"o\">.</span><span class=\"n\">BytesIO</span><span class=\"p\">()</span>\n\n    <span class=\"c1\"># Create the PDF object, using the buffer as its &quot;file.&quot;</span>\n    <span class=\"n\">p</span> <span class=\"o\">=</span> <span class=\"n\">canvas</span><span class=\"o\">.</span><span class=\"n\">Canvas</span><span class=\"p\">(</span><span class=\"n\">buffer</span><span class=\"p\">)</span>\n\n    <span class=\"c1\"># Draw things on the PDF. Here&#39;s where the PDF generation happens.</span>\n    <span class=\"c1\"># See the ReportLab documentation for the full list of functionality.</span>\n    <span class=\"n\">p</span><span class=\"o\">.</span><span class=\"n\">drawString</span><span class=\"p\">(</span><span class=\"mi\">100</span><span class=\"p\">,</span> <span class=\"mi\">100</span><span class=\"p\">,</span> <span class=\"s2\">&quot;Hello world.&quot;</span><span class=\"p\">)</span>\n\n    <span class=\"c1\"># Close the PDF object cleanly, and we&#39;re done.</span>\n    <span class=\"n\">p</span><span class=\"o\">.</span><span class=\"n\">showPage</span><span class=\"p\">()</span>\n    <span class=\"n\">p</span><span class=\"o\">.</span><span class=\"n\">save</span><span class=\"p\">()</span>\n\n    <span class=\"c1\"># FileResponse sets the Content-Disposition header so that browsers</span>\n    <span class=\"c1\"># present the option to save the file.</span>\n    <span class=\"n\">buffer</span><span class=\"o\">.</span><span class=\"n\">seek</span><span class=\"p\">(</span><span class=\"mi\">0</span><span class=\"p\">)</span>\n    <span class=\"k\">return</span> <span class=\"n\">FileResponse</span><span class=\"p\">(</span><span class=\"n\">buffer</span><span class=\"p\">,</span> <span class=\"n\">as_attachment</span><span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">,</span> <span class=\"n\">filename</span><span class=\"o\">=</span><span class=\"s1\">&#39;hello.pdf&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>The code and comments should be self-explanatory, but a few things deserve a\nmention:</p>\n<ul class=\"simple\">\n<li><p>The response will automatically set the MIME type <em class=\"mimetype\">application/pdf</em>\nbased on the filename extension. This tells browsers that the document is a\nPDF file, rather than an HTML file or a generic\n<em class=\"mimetype\">application/octet-stream</em> binary content.</p></li>\n<li><p>When <code class=\"docutils literal notranslate\"><span class=\"pre\">as_attachment=True</span></code> is passed to <code class=\"docutils literal notranslate\"><span class=\"pre\">FileResponse</span></code>, it sets the\nappropriate <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Disposition</span></code> header and that tells Web browsers to\npop-up a dialog box prompting/confirming how to handle the document even if a\ndefault is set on the machine. If the <code class=\"docutils literal notranslate\"><span class=\"pre\">as_attachment</span></code> parameter is omitted,\nbrowsers will handle the PDF using whatever program/plugin they’ve been\nconfigured to use for PDFs.</p></li>\n<li><p>You can provide an arbitrary <code class=\"docutils literal notranslate\"><span class=\"pre\">filename</span></code> parameter. It’ll be used by browsers\nin the „Save as…” dialog.</p></li>\n<li><p>You can hook into the ReportLab API: The same buffer passed as the first\nargument to <code class=\"docutils literal notranslate\"><span class=\"pre\">canvas.Canvas</span></code> can be fed to the\n<a class=\"reference internal\" href=\"/pl/3.2/ref/request-response/#django.http.FileResponse\" title=\"django.http.FileResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">FileResponse</span></code></a> class.</p></li>\n<li><p>Note that all subsequent PDF-generation methods are called on the PDF\nobject (in this case, <code class=\"docutils literal notranslate\"><span class=\"pre\">p</span></code>) – not on <code class=\"docutils literal notranslate\"><span class=\"pre\">buffer</span></code>.</p></li>\n<li><p>Ostatecznie, to ważne, aby wywołać <code class=\"docutils literal notranslate\"><span class=\"pre\">showPage()</span></code> i <code class=\"docutils literal notranslate\"><span class=\"pre\">save()</span></code> na pliku PDF</p></li>\n</ul>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Informacja</p>\n<p>ReportLab is not thread-safe. Some of our users have reported odd issues\nwith building PDF-generating Django views that are accessed by many people\nat the same time.</p>\n</aside>\n</section>\n<section id=\"other-formats\">\n<h2>Inne formaty<a class=\"heading-anchor\" href=\"#other-formats\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Notice that there isn’t a lot in these examples that’s PDF-specific – just the\nbits using <code class=\"docutils literal notranslate\"><span class=\"pre\">reportlab</span></code>. You can use a similar technique to generate any\narbitrary format that you can find a Python library for. Also see\n<a class=\"reference internal\" href=\"/pl/3.2/howto/outputting-csv/\"><span class=\"doc\">Tworzenie plików CSV w Django</span></a> for another example and some techniques you can use\nwhen generated text-based formats.</p>\n<aside class=\"admonition admonition-seealso\">\n<p class=\"admonition-title\">Zobacz także</p>\n<p>Django Packages provides a <a class=\"reference external\" href=\"https://djangopackages.org/grids/g/pdf/\">comparison of packages</a> that help generate PDF files\nfrom Django.</p>\n</aside>\n</section>","rootId":"outputting-pdfs-with-django","toc":[{"title":"Zainstaluj ReportLab","anchor":"install-reportlab","children":[]},{"title":"Napisz swój widok","anchor":"write-your-view","children":[]},{"title":"Inne formaty","anchor":"other-formats","children":[]}],"breadcrumbs":[{"docname":"howto/index","title":"Przewodniki „Jak to zrobić”","url":"/pl/3.2/howto/"}],"prev":{"docname":"howto/outputting-csv","title":"Tworzenie plików CSV w Django","url":"/pl/3.2/howto/outputting-csv/"},"next":{"docname":"howto/overriding-templates","title":"Nadpisywanie szablonów","url":"/pl/3.2/howto/overriding-templates/"},"formats":{"html":"/pl/3.2/howto/outputting-pdf/","markdown":"/pl/3.2/howto/outputting-pdf.md","json":"/pl/3.2/howto/outputting-pdf.json"},"source":"https://github.com/django/django/blob/stable/3.2.x/docs/howto/outputting-pdf.txt","official":"https://docs.djangoproject.com/pl/3.2/howto/outputting-pdf/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}