[{"data":1,"prerenderedAt":345},["Reactive",2],{"/guides/python/aiohttp/adding-a-custom-header-or-footer":3,"related:AfXiCWYqGB":263},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"language":10,"library":5,"property":11,"output":12,"related":13,"default":6,"body":18,"_type":258,"_id":259,"_source":260,"_file":261,"_extension":262},"/guides/python/aiohttp/adding-a-custom-header-or-footer","aiohttp",false,"","Adding a custom header or footer","Discover how to add custom headers or footers to a PDF using Python and the aiohttp library. This in-depth guide includes Python codes that you can easily follow and quickly generate documents with PDFShift's API.","Python","header","pdf",[14,15,16,17],"loading-css-from-a-string","loading-css-from-a-url","loading-javascript-from-a-string","loading-javascript-from-a-url",{"type":19,"children":20,"toc":255},"root",[21,29,58,70,140,151,156,168,179,184,250],{"type":22,"tag":23,"props":24,"children":25},"element","p",{},[26],{"type":27,"value":28},"text","In this guide, we'll show you how to add custom headers or footers to a PDF using Python and the aiohttp library.",{"type":22,"tag":23,"props":30,"children":31},{},[32,34,40,42,48,50,56],{"type":27,"value":33},"We'll focus this guide on the ",{"type":22,"tag":35,"props":36,"children":38},"code",{"className":37},[],[39],{"type":27,"value":11},{"type":27,"value":41}," parameter, but know that the ",{"type":22,"tag":35,"props":43,"children":45},{"className":44},[],[46],{"type":27,"value":47},"footer",{"type":27,"value":49}," parameter works ",{"type":22,"tag":51,"props":52,"children":53},"em",{},[54],{"type":27,"value":55},"exactly",{"type":27,"value":57}," the same way.",{"type":22,"tag":23,"props":59,"children":60},{},[61,63,68],{"type":27,"value":62},"The ",{"type":22,"tag":35,"props":64,"children":66},{"className":65},[],[67],{"type":27,"value":11},{"type":27,"value":69}," parameter is an object that accepts the following parameters:",{"type":22,"tag":71,"props":72,"children":73},"ul",{},[74,86,129],{"type":22,"tag":75,"props":76,"children":77},"li",{},[78,84],{"type":22,"tag":35,"props":79,"children":81},{"className":80},[],[82],{"type":27,"value":83},"source",{"type":27,"value":85},": The source of the header. It can be a URL or a raw HTML document. You can also provide some variables that we'll explain at the bottom of this guide.",{"type":22,"tag":75,"props":87,"children":88},{},[89,95,97,103,105,111,113,119,121,127],{"type":22,"tag":35,"props":90,"children":92},{"className":91},[],[93],{"type":27,"value":94},"height",{"type":27,"value":96},": The height of the header. By default, the height is in pixels, but you can also use ",{"type":22,"tag":35,"props":98,"children":100},{"className":99},[],[101],{"type":27,"value":102},"mm",{"type":27,"value":104},", ",{"type":22,"tag":35,"props":106,"children":108},{"className":107},[],[109],{"type":27,"value":110},"cm",{"type":27,"value":112}," or ",{"type":22,"tag":35,"props":114,"children":116},{"className":115},[],[117],{"type":27,"value":118},"in",{"type":27,"value":120}," as units, like ",{"type":22,"tag":35,"props":122,"children":124},{"className":123},[],[125],{"type":27,"value":126},"10mm",{"type":27,"value":128},".",{"type":22,"tag":75,"props":130,"children":131},{},[132,138],{"type":22,"tag":35,"props":133,"children":135},{"className":134},[],[136],{"type":27,"value":137},"start_at",{"type":27,"value":139},": The page number where the header should start. By default, the header will start at the first page.",{"type":22,"tag":23,"props":141,"children":142},{},[143,149],{"type":22,"tag":144,"props":145,"children":146},"strong",{},[147],{"type":27,"value":148},"NOTE",{"type":27,"value":150},": You must provide the full data in the header/footer, and not via a network request. Loading files such as external CSS, Js or Fonts won't work in the header or footer.\nInstead, we recommend you to embed them in Base64.",{"type":22,"tag":23,"props":152,"children":153},{},[154],{"type":27,"value":155},"Here's an example:",{"type":22,"tag":157,"props":158,"children":163},"pre",{"className":159,"code":161,"language":162,"meta":7},[160],"language-python","import aiohttp, asyncio, json\n\n# You can get an API key at https://pdfshift.io\napi_key = 'sk_xxxxxxxxxxxx'\n\nparams = {\n    'source': 'https://en.wikipedia.org/wiki/PDF',\n    'header': {\n        'source': '\u003Cdiv>Page {{ page }} over a total of {{ total }}. Made on {{ date }}\u003C/div>',\n        'height': 150,\n        'start_at': 2\n    }\n}\n\nresponse = None\ntry:\n    async with aiohttp.ClientSession() as session:\n        async with session.post(\n            'https://api.pdfshift.io/v3/convert/pdf',\n            headers={'X-API-Key': api_key},\n            json=params\n        ) as response:\n            if response.status >= 400:\n                raise Exception('Invalid request: {}'.format(await response.text()))\n\n            with open('result.pdf', 'wb') as f:\n                f.write(await response.read())\n\n            print('The PDF document was generated and saved to result.pdf')\nexcept asyncio.TimeoutError:\n    raise Exception('The request took too long to process')\nexcept aiohttp.ClientError as e:\n    raise Exception(f'An error occurred: {e}')\nexcept Exception as e:\n    # We highly recommend you to handle exceptions. Often, PDFShift will provide you with a clear explanation about what happened.\n    # Moreover, in case of error, no PDF are returned !\n    raise Exception(f'An error occurred: {e}')\n","python",[164],{"type":22,"tag":35,"props":165,"children":166},{"__ignoreMap":7},[167],{"type":27,"value":161},{"type":22,"tag":23,"props":169,"children":170},{},[171,172,177],{"type":27,"value":62},{"type":22,"tag":35,"props":173,"children":175},{"className":174},[],[176],{"type":27,"value":83},{"type":27,"value":178}," parameter present in the header/footer accepts a set of variables that will be translated when converting the document.",{"type":22,"tag":23,"props":180,"children":181},{},[182],{"type":27,"value":183},"Here are the properties you can use:",{"type":22,"tag":71,"props":185,"children":186},{},[187,198,209,220,231],{"type":22,"tag":75,"props":188,"children":189},{},[190,196],{"type":22,"tag":35,"props":191,"children":193},{"className":192},[],[194],{"type":27,"value":195},"{{ title }}",{"type":27,"value":197},": The title of the document.",{"type":22,"tag":75,"props":199,"children":200},{},[201,207],{"type":22,"tag":35,"props":202,"children":204},{"className":203},[],[205],{"type":27,"value":206},"{{ url }}",{"type":27,"value":208},": The URL of the document.",{"type":22,"tag":75,"props":210,"children":211},{},[212,218],{"type":22,"tag":35,"props":213,"children":215},{"className":214},[],[216],{"type":27,"value":217},"{{ page }}",{"type":27,"value":219},": The current page number.",{"type":22,"tag":75,"props":221,"children":222},{},[223,229],{"type":22,"tag":35,"props":224,"children":226},{"className":225},[],[227],{"type":27,"value":228},"{{ total }}",{"type":27,"value":230},": The total number of pages in the document.",{"type":22,"tag":75,"props":232,"children":233},{},[234,240,242,248],{"type":22,"tag":35,"props":235,"children":237},{"className":236},[],[238],{"type":27,"value":239},"{{ date }}",{"type":27,"value":241},": The current date in the format ",{"type":22,"tag":35,"props":243,"children":245},{"className":244},[],[246],{"type":27,"value":247},"M/D/YY-H:MM am/pm",{"type":27,"value":249}," such as \"3/16/24, 2:04 PM\".",{"type":22,"tag":23,"props":251,"children":252},{},[253],{"type":27,"value":254},"This will allow you to generate a document that looks more like a printable version of a website, with page number and customized header and footer.",{"title":7,"searchDepth":256,"depth":256,"links":257},2,[],"markdown","content:guides:python:aiohttp:adding-a-custom-header-or-footer.md","content","guides/python/aiohttp/adding-a-custom-header-or-footer.md","md",[264,268,269,273,277,281,285,289,293,297,301,305,309,313,317,321,325,329,333,337,341],{"_path":265,"title":266,"language":10,"library":5,"_id":267},"/guides/python/aiohttp/accessing-secured-pages","Accessing secured pages","content:guides:python:aiohttp:accessing-secured-pages.md",{"_path":4,"title":8,"language":10,"library":5,"_id":259},{"_path":270,"title":271,"language":10,"library":5,"_id":272},"/guides/python/aiohttp/adding-a-text-watermark","Adding a text watermark","content:guides:python:aiohttp:adding-a-text-watermark.md",{"_path":274,"title":275,"language":10,"library":5,"_id":276},"/guides/python/aiohttp/adding-an-image-watermark","Adding an image watermark","content:guides:python:aiohttp:adding-an-image-watermark.md",{"_path":278,"title":279,"language":10,"library":5,"_id":280},"/guides/python/aiohttp/avoid-conversion-on-error","Avoid the conversion on error when loading the document","content:guides:python:aiohttp:avoid-conversion-on-error.md",{"_path":282,"title":283,"language":10,"library":5,"_id":284},"/guides/python/aiohttp/convert-html-to-pdf-from-a-url","Convert an HTML document to PDF from a URL","content:guides:python:aiohttp:convert-html-to-pdf-from-a-url.md",{"_path":286,"title":287,"language":10,"library":5,"_id":288},"/guides/python/aiohttp/convert-html-to-pdf-from-raw-html","Convert an HTML document to PDF from raw HTML","content:guides:python:aiohttp:convert-html-to-pdf-from-raw-html.md",{"_path":290,"title":291,"language":10,"library":5,"_id":292},"/guides/python/aiohttp/define-a-time-limit","Define a time limit","content:guides:python:aiohttp:define-a-time-limit.md",{"_path":294,"title":295,"language":10,"library":5,"_id":296},"/guides/python/aiohttp/exporting-only-a-specific-set-of-pages","Exporting only a specific set of pages","content:guides:python:aiohttp:exporting-only-a-specific-set-of-pages.md",{"_path":298,"title":299,"language":10,"library":5,"_id":300},"/guides/python/aiohttp/generate-a-document-with-full-height","Generate a document with full height","content:guides:python:aiohttp:generate-a-document-with-full-height.md",{"_path":302,"title":303,"language":10,"library":5,"_id":304},"/guides/python/aiohttp/loading-css-from-a-string","Loading CSS from a string","content:guides:python:aiohttp:loading-css-from-a-string.md",{"_path":306,"title":307,"language":10,"library":5,"_id":308},"/guides/python/aiohttp/loading-css-from-a-url","Loading CSS from a URL","content:guides:python:aiohttp:loading-css-from-a-url.md",{"_path":310,"title":311,"language":10,"library":5,"_id":312},"/guides/python/aiohttp/loading-javascript-from-a-string","Loading Javascript from a string","content:guides:python:aiohttp:loading-javascript-from-a-string.md",{"_path":314,"title":315,"language":10,"library":5,"_id":316},"/guides/python/aiohttp/loading-javascript-from-a-url","Loading Javascript from a URL","content:guides:python:aiohttp:loading-javascript-from-a-url.md",{"_path":318,"title":319,"language":10,"library":5,"_id":320},"/guides/python/aiohttp/protecting-the-generated-pdf","Protecting the generated PDF","content:guides:python:aiohttp:protecting-the-generated-pdf.md",{"_path":322,"title":323,"language":10,"library":5,"_id":324},"/guides/python/aiohttp/receive-a-webhook-event","Receive a webhook event","content:guides:python:aiohttp:receive-a-webhook-event.md",{"_path":326,"title":327,"language":10,"library":5,"_id":328},"/guides/python/aiohttp/save-your-pdf-online-and-get-back-a-url","Save your PDF online and get back a URL","content:guides:python:aiohttp:save-your-pdf-online-and-get-back-a-url.md",{"_path":330,"title":331,"language":10,"library":5,"_id":332},"/guides/python/aiohttp/save-your-pdf-to-your-amazon-s3-bucket","Save your PDF to your Amazon S3 Bucket","content:guides:python:aiohttp:save-your-pdf-to-your-amazon-s3-bucket.md",{"_path":334,"title":335,"language":10,"library":5,"_id":336},"/guides/python/aiohttp/send-custom-http-headers","Send custom HTTP headers","content:guides:python:aiohttp:send-custom-http-headers.md",{"_path":338,"title":339,"language":10,"library":5,"_id":340},"/guides/python/aiohttp/using-cookies","Using cookies to convert HTML documents to PDF","content:guides:python:aiohttp:using-cookies.md",{"_path":342,"title":343,"language":10,"library":5,"_id":344},"/guides/python/aiohttp/waiting-for-a-custom-element-to-be-ready","Waiting for a custom element to be ready","content:guides:python:aiohttp:waiting-for-a-custom-element-to-be-ready.md",1785426832858]