[{"data":1,"prerenderedAt":141},["Reactive",2],{"/guides/java/httpclient/adding-a-custom-header-or-footer":3,"/guides/java/httpclient/adding-a-custom-header-or-footer-related":59},{"_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":6,"body":17,"_type":54,"_id":55,"_source":56,"_file":57,"_extension":58},"/guides/java/httpclient/adding-a-custom-header-or-footer","httpclient",false,"","Adding a custom header or footer","Learn how to add custom headers and footers to your PDFs using Java and the HttpClient library. This guide offers detailed steps with code samples in Java and the HttpClient library, showing how to customize PDF appearance with headers and footers.","Java","HttpClient","header","pdf",[15,16],"loading-css-from-a-string","loading-css-from-a-url",{"type":18,"children":19,"toc":51},"root",[20,28,33,46],{"type":21,"tag":22,"props":23,"children":24},"element","p",{},[25],{"type":26,"value":27},"text","In this guide, we'll show you how to add custom headers and footers to your PDFs using Java and the HttpClient library.",{"type":21,"tag":22,"props":29,"children":30},{},[31],{"type":26,"value":32},"When generating PDFs, you might want to add custom headers and footers to your documents for branding or informational purposes.",{"type":21,"tag":34,"props":35,"children":40},"pre",{"className":36,"code":38,"language":39,"meta":7},[37],"language-java","import java.net.http.*;\nimport java.net.URI;\nimport java.net.http.HttpRequest.BodyPublishers;\nimport java.net.http.HttpResponse.BodyHandlers;\nimport java.time.Duration;\n\n// You can get an API key at https://pdfshift.io\nString apiKey = \"sk_xxxxxxxxxxxx\";\n\nHttpClient client = HttpClient.newBuilder()\n    .connectTimeout(Duration.ofSeconds(10))\n    .build();\n\nHttpRequest request = HttpRequest.newBuilder()\n    .uri(URI.create(\"https://api.pdfshift.io/v3/convert/pdf\"))\n    .header(\"X-API-Key\", apiKey)\n    .header(\"Content-Type\", \"application/json\")\n    .POST(BodyPublishers.ofString(\"{\\n\" +\n        \"  \\\"source\\\": \\\"https://www.example.com\\\",\\n\" +\n        \"  \\\"header\\\": {\\n\" +\n        \"    \\\"content\\\": \\\"\u003Cdiv style='text-align: center;'>Header Content\u003C/div>\\\",\\n\" +\n        \"    \\\"height\\\": \\\"20mm\\\"\\n\" +\n        \"  },\\n\" +\n        \"  \\\"footer\\\": {\\n\" +\n        \"    \\\"content\\\": \\\"\u003Cdiv style='text-align: center;'>Footer Content\u003C/div>\\\",\\n\" +\n        \"    \\\"height\\\": \\\"20mm\\\"\\n\" +\n        \"  }\\n\" +\n        \"}\"))\n    .build();\n\nHttpResponse\u003Cbyte[]> response = client.send(request, BodyHandlers.ofByteArray());\n\n// Handle errors:\nif (response.statusCode() >= 400) {\n    throw new RuntimeException(\"Request failed with status code \" + response.statusCode());\n}\n\njava.nio.file.Files.write(java.nio.file.Paths.get(\"result.pdf\"), response.body());\n\nSystem.out.println(\"The PDF document was generated and saved to result.pdf\");\n","java",[41],{"type":21,"tag":42,"props":43,"children":44},"code",{"__ignoreMap":7},[45],{"type":26,"value":38},{"type":21,"tag":22,"props":47,"children":48},{},[49],{"type":26,"value":50},"This allows you to add consistent branding elements to all your generated PDF documents.",{"title":7,"searchDepth":52,"depth":52,"links":53},2,[],"markdown","content:guides:java:httpclient:adding-a-custom-header-or-footer.md","content","guides/java/httpclient/adding-a-custom-header-or-footer.md","md",[60,64,65,69,73,77,81,85,89,93,97,101,105,109,113,117,121,125,129,133,137],{"_path":61,"title":62,"language":10,"library":11,"_id":63},"/guides/java/httpclient/accessing-secured-pages","Accessing secured pages","content:guides:java:httpclient:accessing-secured-pages.md",{"_path":4,"title":8,"language":10,"library":11,"_id":55},{"_path":66,"title":67,"language":10,"library":11,"_id":68},"/guides/java/httpclient/adding-a-text-watermark","Adding a text watermark","content:guides:java:httpclient:adding-a-text-watermark.md",{"_path":70,"title":71,"language":10,"library":11,"_id":72},"/guides/java/httpclient/adding-an-image-watermark","Adding an image watermark","content:guides:java:httpclient:adding-an-image-watermark.md",{"_path":74,"title":75,"language":10,"library":11,"_id":76},"/guides/java/httpclient/avoid-conversion-on-error","Avoid conversion on error","content:guides:java:httpclient:avoid-conversion-on-error.md",{"_path":78,"title":79,"language":10,"library":11,"_id":80},"/guides/java/httpclient/convert-html-to-pdf-from-a-url","Convert HTML to PDF from a URL","content:guides:java:httpclient:convert-html-to-pdf-from-a-url.md",{"_path":82,"title":83,"language":10,"library":11,"_id":84},"/guides/java/httpclient/convert-html-to-pdf-from-raw-html","Convert HTML to PDF from raw HTML","content:guides:java:httpclient:convert-html-to-pdf-from-raw-html.md",{"_path":86,"title":87,"language":10,"library":11,"_id":88},"/guides/java/httpclient/define-a-time-limit","Define a time limit","content:guides:java:httpclient:define-a-time-limit.md",{"_path":90,"title":91,"language":10,"library":11,"_id":92},"/guides/java/httpclient/exporting-only-a-specific-set-of-pages","Exporting only a specific set of pages","content:guides:java:httpclient:exporting-only-a-specific-set-of-pages.md",{"_path":94,"title":95,"language":10,"library":11,"_id":96},"/guides/java/httpclient/generate-a-document-with-full-height","Generate a document with full height","content:guides:java:httpclient:generate-a-document-with-full-height.md",{"_path":98,"title":99,"language":10,"library":11,"_id":100},"/guides/java/httpclient/loading-css-from-a-string","Loading CSS from a string","content:guides:java:httpclient:loading-css-from-a-string.md",{"_path":102,"title":103,"language":10,"library":11,"_id":104},"/guides/java/httpclient/loading-css-from-a-url","Loading CSS from a URL","content:guides:java:httpclient:loading-css-from-a-url.md",{"_path":106,"title":107,"language":10,"library":11,"_id":108},"/guides/java/httpclient/loading-javascript-from-a-string","Loading JavaScript from a string","content:guides:java:httpclient:loading-javascript-from-a-string.md",{"_path":110,"title":111,"language":10,"library":11,"_id":112},"/guides/java/httpclient/loading-javascript-from-a-url","Loading JavaScript from a URL","content:guides:java:httpclient:loading-javascript-from-a-url.md",{"_path":114,"title":115,"language":10,"library":11,"_id":116},"/guides/java/httpclient/protecting-the-generated-pdf","Protecting the generated PDF","content:guides:java:httpclient:protecting-the-generated-pdf.md",{"_path":118,"title":119,"language":10,"library":11,"_id":120},"/guides/java/httpclient/receive-a-webhook-event","Receive a webhook event","content:guides:java:httpclient:receive-a-webhook-event.md",{"_path":122,"title":123,"language":10,"library":11,"_id":124},"/guides/java/httpclient/save-your-pdf-online-and-get-back-a-url","Save your PDF online and get back a URL","content:guides:java:httpclient:save-your-pdf-online-and-get-back-a-url.md",{"_path":126,"title":127,"language":10,"library":11,"_id":128},"/guides/java/httpclient/save-your-pdf-to-your-amazon-s3-bucket","Save your PDF to your Amazon S3 bucket","content:guides:java:httpclient:save-your-pdf-to-your-amazon-s3-bucket.md",{"_path":130,"title":131,"language":10,"library":11,"_id":132},"/guides/java/httpclient/send-custom-http-headers","Send custom HTTP headers","content:guides:java:httpclient:send-custom-http-headers.md",{"_path":134,"title":135,"language":10,"library":11,"_id":136},"/guides/java/httpclient/using-cookies","Using cookies","content:guides:java:httpclient:using-cookies.md",{"_path":138,"title":139,"language":10,"library":11,"_id":140},"/guides/java/httpclient/waiting-for-a-custom-element-to-be-ready","Waiting for a custom element to be ready","content:guides:java:httpclient:waiting-for-a-custom-element-to-be-ready.md",1780405100883]