[{"data":1,"prerenderedAt":347},["Reactive",2],{"/guides/go/nethttp/adding-a-custom-header-or-footer":3,"related:E6xZClvgPX":265},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"language":10,"library":11,"property":12,"output":13,"related":14,"default":19,"body":20,"_type":260,"_id":261,"_source":262,"_file":263,"_extension":264},"/guides/go/nethttp/adding-a-custom-header-or-footer","nethttp",false,"","Adding a custom header or footer","Discover how to add custom headers or footers to a PDF using Go and the Net/HTTP library. This in-depth guide includes Go codes that you can easily follow and quickly generate documents with PDFShift's API.","Go","Net/HTTP","header","pdf",[15,16,17,18],"loading-css-from-a-string","loading-css-from-a-url","loading-javascript-from-a-string","loading-javascript-from-a-url",true,{"type":21,"children":22,"toc":257},"root",[23,31,60,72,142,153,158,170,181,186,252],{"type":24,"tag":25,"props":26,"children":27},"element","p",{},[28],{"type":29,"value":30},"text","In this guide, we'll show you how to add custom headers or footers to a PDF using Go and the Net/HTTP library.",{"type":24,"tag":25,"props":32,"children":33},{},[34,36,42,44,50,52,58],{"type":29,"value":35},"We'll focus this guide on the ",{"type":24,"tag":37,"props":38,"children":40},"code",{"className":39},[],[41],{"type":29,"value":12},{"type":29,"value":43}," parameter, but know that the ",{"type":24,"tag":37,"props":45,"children":47},{"className":46},[],[48],{"type":29,"value":49},"footer",{"type":29,"value":51}," parameter works ",{"type":24,"tag":53,"props":54,"children":55},"em",{},[56],{"type":29,"value":57},"exactly",{"type":29,"value":59}," the same way.",{"type":24,"tag":25,"props":61,"children":62},{},[63,65,70],{"type":29,"value":64},"The ",{"type":24,"tag":37,"props":66,"children":68},{"className":67},[],[69],{"type":29,"value":12},{"type":29,"value":71}," parameter is an object that accepts the following parameters:",{"type":24,"tag":73,"props":74,"children":75},"ul",{},[76,88,131],{"type":24,"tag":77,"props":78,"children":79},"li",{},[80,86],{"type":24,"tag":37,"props":81,"children":83},{"className":82},[],[84],{"type":29,"value":85},"source",{"type":29,"value":87},": 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":24,"tag":77,"props":89,"children":90},{},[91,97,99,105,107,113,115,121,123,129],{"type":24,"tag":37,"props":92,"children":94},{"className":93},[],[95],{"type":29,"value":96},"height",{"type":29,"value":98},": The height of the header. By default, the height is in pixels, but you can also use ",{"type":24,"tag":37,"props":100,"children":102},{"className":101},[],[103],{"type":29,"value":104},"mm",{"type":29,"value":106},", ",{"type":24,"tag":37,"props":108,"children":110},{"className":109},[],[111],{"type":29,"value":112},"cm",{"type":29,"value":114}," or ",{"type":24,"tag":37,"props":116,"children":118},{"className":117},[],[119],{"type":29,"value":120},"in",{"type":29,"value":122}," as units, like ",{"type":24,"tag":37,"props":124,"children":126},{"className":125},[],[127],{"type":29,"value":128},"10mm",{"type":29,"value":130},".",{"type":24,"tag":77,"props":132,"children":133},{},[134,140],{"type":24,"tag":37,"props":135,"children":137},{"className":136},[],[138],{"type":29,"value":139},"start_at",{"type":29,"value":141},": The page number where the header should start. By default, the header will start at the first page.",{"type":24,"tag":25,"props":143,"children":144},{},[145,151],{"type":24,"tag":146,"props":147,"children":148},"strong",{},[149],{"type":29,"value":150},"NOTE",{"type":29,"value":152},": 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":24,"tag":25,"props":154,"children":155},{},[156],{"type":29,"value":157},"Here's an example:",{"type":24,"tag":159,"props":160,"children":165},"pre",{"className":161,"code":163,"language":164,"meta":7},[162],"language-go","package main\n\nimport (\n    \"bytes\"\n    \"encoding/json\"\n    \"fmt\"\n    \"io/ioutil\"\n    \"net/http\"\n)\n\nfunc main() {\n    // Define the API key\n    apiKey := \"sk_xxxxxxxxxxxx\"\n\n    // Define the request parameters\n    params := map[string]interface{}{\n        \"source\": \"https://en.wikipedia.org/wiki/PDF\",\n        \"header\": map[string]interface{}{\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\n    // Marshal the parameters into JSON\n    jsonParams, err := json.Marshal(params)\n    if err != nil {\n        fmt.Println(\"Error marshaling JSON:\", err)\n        return\n    }\n\n    // Create a new HTTP client\n    client := &http.Client{}\n\n    // Create a new request\n    req, err := http.NewRequest(\"POST\", \"https://api.pdfshift.io/v3/convert/pdf\", bytes.NewBuffer(jsonParams))\n    if err != nil {\n        fmt.Println(\"Error creating request:\", err)\n        return\n    }\n\n    // Set request headers\n    req.Header.Set(\"Content-Type\", \"application/json\")\n\n    // Set basic authentication header\n    req.Header.Set(\"X-API-Key\", apiKey)\n\n    // Perform the request\n    resp, err := client.Do(req)\n    if err != nil {\n        fmt.Println(\"Error performing request:\", err)\n        return\n    }\n    defer resp.Body.Close()\n\n    // Read response body\n    body, err := ioutil.ReadAll(resp.Body)\n    if err != nil {\n        fmt.Println(\"Error reading response body:\", err)\n        return\n    }\n\n    // Check response status code\n    if resp.StatusCode >= 400 {\n        fmt.Printf(\"Request failed with status code %d: %s\\n\", resp.StatusCode, string(body))\n        return\n    }\n\n    // Save the PDF document\n    err = ioutil.WriteFile(\"result.pdf\", body, 0644)\n    if err != nil {\n        fmt.Println(\"Error saving PDF document:\", err)\n        return\n    }\n\n    fmt.Println(\"The PDF document was generated and saved to result.pdf\")\n}\n","go",[166],{"type":24,"tag":37,"props":167,"children":168},{"__ignoreMap":7},[169],{"type":29,"value":163},{"type":24,"tag":25,"props":171,"children":172},{},[173,174,179],{"type":29,"value":64},{"type":24,"tag":37,"props":175,"children":177},{"className":176},[],[178],{"type":29,"value":85},{"type":29,"value":180}," parameter present in the header/footer accepts a set of variables that will be translated when converting the document.",{"type":24,"tag":25,"props":182,"children":183},{},[184],{"type":29,"value":185},"Here are the properties you can use:",{"type":24,"tag":73,"props":187,"children":188},{},[189,200,211,222,233],{"type":24,"tag":77,"props":190,"children":191},{},[192,198],{"type":24,"tag":37,"props":193,"children":195},{"className":194},[],[196],{"type":29,"value":197},"{{ title }}",{"type":29,"value":199},": The title of the document.",{"type":24,"tag":77,"props":201,"children":202},{},[203,209],{"type":24,"tag":37,"props":204,"children":206},{"className":205},[],[207],{"type":29,"value":208},"{{ url }}",{"type":29,"value":210},": The URL of the document.",{"type":24,"tag":77,"props":212,"children":213},{},[214,220],{"type":24,"tag":37,"props":215,"children":217},{"className":216},[],[218],{"type":29,"value":219},"{{ page }}",{"type":29,"value":221},": The current page number.",{"type":24,"tag":77,"props":223,"children":224},{},[225,231],{"type":24,"tag":37,"props":226,"children":228},{"className":227},[],[229],{"type":29,"value":230},"{{ total }}",{"type":29,"value":232},": The total number of pages in the document.",{"type":24,"tag":77,"props":234,"children":235},{},[236,242,244,250],{"type":24,"tag":37,"props":237,"children":239},{"className":238},[],[240],{"type":29,"value":241},"{{ date }}",{"type":29,"value":243},": The current date in the format ",{"type":24,"tag":37,"props":245,"children":247},{"className":246},[],[248],{"type":29,"value":249},"M/D/YY-H:MM am/pm",{"type":29,"value":251}," such as \"3/16/24, 2:04 PM\".",{"type":24,"tag":25,"props":253,"children":254},{},[255],{"type":29,"value":256},"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":258,"depth":258,"links":259},2,[],"markdown","content:guides:go:nethttp:adding-a-custom-header-or-footer.md","content","guides/go/nethttp/adding-a-custom-header-or-footer.md","md",[266,270,271,275,279,283,287,291,295,299,303,307,311,315,319,323,327,331,335,339,343],{"_path":267,"title":268,"language":10,"library":11,"_id":269},"/guides/go/nethttp/accessing-secured-pages","Accessing secured pages","content:guides:go:nethttp:accessing-secured-pages.md",{"_path":4,"title":8,"language":10,"library":11,"_id":261},{"_path":272,"title":273,"language":10,"library":11,"_id":274},"/guides/go/nethttp/adding-a-text-watermark","Adding a text watermark","content:guides:go:nethttp:adding-a-text-watermark.md",{"_path":276,"title":277,"language":10,"library":11,"_id":278},"/guides/go/nethttp/adding-an-image-watermark","Adding an image watermark","content:guides:go:nethttp:adding-an-image-watermark.md",{"_path":280,"title":281,"language":10,"library":11,"_id":282},"/guides/go/nethttp/avoid-conversion-on-error","Avoid the conversion on error when loading the document","content:guides:go:nethttp:avoid-conversion-on-error.md",{"_path":284,"title":285,"language":10,"library":11,"_id":286},"/guides/go/nethttp/convert-html-to-pdf-from-a-url","Convert an HTML document to PDF from a URL","content:guides:go:nethttp:convert-html-to-pdf-from-a-url.md",{"_path":288,"title":289,"language":10,"library":11,"_id":290},"/guides/go/nethttp/convert-html-to-pdf-from-raw-html","Convert an HTML document to PDF from raw HTML","content:guides:go:nethttp:convert-html-to-pdf-from-raw-html.md",{"_path":292,"title":293,"language":10,"library":11,"_id":294},"/guides/go/nethttp/define-a-time-limit","Define a time limit","content:guides:go:nethttp:define-a-time-limit.md",{"_path":296,"title":297,"language":10,"library":11,"_id":298},"/guides/go/nethttp/exporting-only-a-specific-set-of-pages","Exporting only a specific set of pages","content:guides:go:nethttp:exporting-only-a-specific-set-of-pages.md",{"_path":300,"title":301,"language":10,"library":11,"_id":302},"/guides/go/nethttp/generate-a-document-with-full-height","Generate a document with full height","content:guides:go:nethttp:generate-a-document-with-full-height.md",{"_path":304,"title":305,"language":10,"library":11,"_id":306},"/guides/go/nethttp/loading-css-from-a-string","Loading CSS from a string","content:guides:go:nethttp:loading-css-from-a-string.md",{"_path":308,"title":309,"language":10,"library":11,"_id":310},"/guides/go/nethttp/loading-css-from-a-url","Loading CSS from a URL","content:guides:go:nethttp:loading-css-from-a-url.md",{"_path":312,"title":313,"language":10,"library":11,"_id":314},"/guides/go/nethttp/loading-javascript-from-a-string","Loading Javascript from a string","content:guides:go:nethttp:loading-javascript-from-a-string.md",{"_path":316,"title":317,"language":10,"library":11,"_id":318},"/guides/go/nethttp/loading-javascript-from-a-url","Loading Javascript from a URL","content:guides:go:nethttp:loading-javascript-from-a-url.md",{"_path":320,"title":321,"language":10,"library":11,"_id":322},"/guides/go/nethttp/protecting-the-generated-pdf","Protecting the generated PDF","content:guides:go:nethttp:protecting-the-generated-pdf.md",{"_path":324,"title":325,"language":10,"library":11,"_id":326},"/guides/go/nethttp/receive-a-webhook-event","Receive a webhook event","content:guides:go:nethttp:receive-a-webhook-event.md",{"_path":328,"title":329,"language":10,"library":11,"_id":330},"/guides/go/nethttp/save-your-pdf-online-and-get-back-a-url","Save your PDF online and get back a URL","content:guides:go:nethttp:save-your-pdf-online-and-get-back-a-url.md",{"_path":332,"title":333,"language":10,"library":11,"_id":334},"/guides/go/nethttp/save-your-pdf-to-your-amazon-s3-bucket","Save your PDF to your Amazon S3 Bucket","content:guides:go:nethttp:save-your-pdf-to-your-amazon-s3-bucket.md",{"_path":336,"title":337,"language":10,"library":11,"_id":338},"/guides/go/nethttp/send-custom-http-headers","Send custom HTTP headers","content:guides:go:nethttp:send-custom-http-headers.md",{"_path":340,"title":341,"language":10,"library":11,"_id":342},"/guides/go/nethttp/using-cookies","Using cookies to convert HTML documents to PDF","content:guides:go:nethttp:using-cookies.md",{"_path":344,"title":345,"language":10,"library":11,"_id":346},"/guides/go/nethttp/waiting-for-a-custom-element-to-be-ready","Waiting for a custom element to be ready","content:guides:go:nethttp:waiting-for-a-custom-element-to-be-ready.md",1785426824553]