{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Multipage PDF\n\nThis is a demo of creating a pdf file with several pages,\nas well as adding metadata and annotations to pdf files.\n\nIf you want to use a multipage pdf file using LaTeX, you need\nto use ``from matplotlib.backends.backend_pgf import PdfPages``.\nThis version however does not support `.attach_note`.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import datetime\n\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nfrom matplotlib.backends.backend_pdf import PdfPages\n\n# Create the PdfPages object to which we will save the pages:\n# The with statement makes sure that the PdfPages object is closed properly at\n# the end of the block, even if an Exception occurs.\nwith PdfPages('multipage_pdf.pdf') as pdf:\n plt.figure(figsize=(3, 3))\n plt.plot(range(7), [3, 1, 4, 1, 5, 9, 2], 'r-o')\n plt.title('Page One')\n pdf.savefig() # saves the current figure into a pdf page\n plt.close()\n\n # if LaTeX is not installed or error caught, change to `False`\n plt.rcParams['text.usetex'] = True\n plt.figure(figsize=(8, 6))\n x = np.arange(0, 5, 0.1)\n plt.plot(x, np.sin(x), 'b-')\n plt.title('Page Two')\n pdf.attach_note(\"plot of sin(x)\") # attach metadata (as pdf note) to page\n pdf.savefig()\n plt.close()\n\n plt.rcParams['text.usetex'] = False\n fig = plt.figure(figsize=(4, 5))\n plt.plot(x, x ** 2, 'ko')\n plt.title('Page Three')\n pdf.savefig(fig) # or you can pass a Figure object to pdf.savefig\n plt.close()\n\n # We can also set the file's metadata via the PdfPages object:\n d = pdf.infodict()\n d['Title'] = 'Multipage PDF Example'\n d['Author'] = 'Jouni K. Sepp\\xe4nen'\n d['Subject'] = 'How to create a multipage pdf file and set its metadata'\n d['Keywords'] = 'PdfPages multipage keywords author title subject'\n d['CreationDate'] = datetime.datetime(2009, 11, 13)\n d['ModDate'] = datetime.datetime.today()" ] } ], "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.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }