[{"data":1,"prerenderedAt":141},["Reactive",2],{"/guides/java/okhttp/loading-css-from-a-url":3,"/guides/java/okhttp/loading-css-from-a-url-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/okhttp/loading-css-from-a-url","okhttp",false,"","Loading CSS from a URL","Learn how to load CSS from a URL when converting HTML to PDF using Java and the OkHTTP library. This guide offers detailed steps with code samples in Java and the OkHTTP library, showing how to reference external CSS files.","Java","OkHTTP","css","pdf",[15,16],"loading-css-from-a-string","convert-html-to-pdf-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 load CSS from a URL when converting HTML to PDF using Java and the OkHTTP library.",{"type":21,"tag":22,"props":29,"children":30},{},[31],{"type":26,"value":32},"When converting HTML to PDF, you might want to reference external CSS files to maintain consistent styling.",{"type":21,"tag":34,"props":35,"children":40},"pre",{"className":36,"code":38,"language":39,"meta":7},[37],"language-java","import okhttp3.*;\nimport java.io.IOException;\n\n// You can get an API key at https://pdfshift.io\nString apiKey = \"sk_xxxxxxxxxxxx\";\n\nOkHttpClient client = new OkHttpClient();\n\nRequestBody body = RequestBody.create(\n    \"{\\n\" +\n    \"  \\\"source\\\": \\\"https://www.example.com\\\",\\n\" +\n    \"  \\\"css\\\": \\\"https://example.com/styles.css\\\"\\n\" +\n    \"}\", MediaType.parse(\"application/json\"));\n\nRequest request = new Request.Builder()\n    .url(\"https://api.pdfshift.io/v3/convert/pdf\")\n    .addHeader(\"X-API-Key\", apiKey)\n    .post(body)\n    .build();\n\ntry (Response response = client.newCall(request).execute()) {\n    \n    // Handle errors:\n    if (!response.isSuccessful()) {\n        throw new IOException(\"Request failed with status code \" + response.code());\n    }\n    \n    ResponseBody responseBody = response.body();\n    if (responseBody != null) {\n        java.nio.file.Files.write(\n            java.nio.file.Paths.get(\"result.pdf\"), \n            responseBody.bytes()\n        );\n    }\n    \n    System.out.println(\"The PDF document was generated and saved to result.pdf\");\n}\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 apply external stylesheets to your PDFs.",{"title":7,"searchDepth":52,"depth":52,"links":53},2,[],"markdown","content:guides:java:okhttp:loading-css-from-a-url.md","content","guides/java/okhttp/loading-css-from-a-url.md","md",[60,64,68,72,76,80,84,88,92,96,100,104,105,109,113,117,121,125,129,133,137],{"_path":61,"title":62,"language":10,"library":11,"_id":63},"/guides/java/okhttp/accessing-secured-pages","Accessing secured pages","content:guides:java:okhttp:accessing-secured-pages.md",{"_path":65,"title":66,"language":10,"library":11,"_id":67},"/guides/java/okhttp/adding-a-custom-header-or-footer","Adding a custom header or footer","content:guides:java:okhttp:adding-a-custom-header-or-footer.md",{"_path":69,"title":70,"language":10,"library":11,"_id":71},"/guides/java/okhttp/adding-a-text-watermark","Adding a text watermark","content:guides:java:okhttp:adding-a-text-watermark.md",{"_path":73,"title":74,"language":10,"library":11,"_id":75},"/guides/java/okhttp/adding-an-image-watermark","Adding an image watermark","content:guides:java:okhttp:adding-an-image-watermark.md",{"_path":77,"title":78,"language":10,"library":11,"_id":79},"/guides/java/okhttp/avoid-conversion-on-error","Avoid conversion on error","content:guides:java:okhttp:avoid-conversion-on-error.md",{"_path":81,"title":82,"language":10,"library":11,"_id":83},"/guides/java/okhttp/convert-html-to-pdf-from-a-url","Convert HTML to PDF from a URL","content:guides:java:okhttp:convert-html-to-pdf-from-a-url.md",{"_path":85,"title":86,"language":10,"library":11,"_id":87},"/guides/java/okhttp/convert-html-to-pdf-from-raw-html","Convert HTML to PDF from raw HTML","content:guides:java:okhttp:convert-html-to-pdf-from-raw-html.md",{"_path":89,"title":90,"language":10,"library":11,"_id":91},"/guides/java/okhttp/define-a-time-limit","Define a time limit","content:guides:java:okhttp:define-a-time-limit.md",{"_path":93,"title":94,"language":10,"library":11,"_id":95},"/guides/java/okhttp/exporting-only-a-specific-set-of-pages","Exporting only a specific set of pages","content:guides:java:okhttp:exporting-only-a-specific-set-of-pages.md",{"_path":97,"title":98,"language":10,"library":11,"_id":99},"/guides/java/okhttp/generate-a-document-with-full-height","Generate a document with full height","content:guides:java:okhttp:generate-a-document-with-full-height.md",{"_path":101,"title":102,"language":10,"library":11,"_id":103},"/guides/java/okhttp/loading-css-from-a-string","Loading CSS from a string","content:guides:java:okhttp:loading-css-from-a-string.md",{"_path":4,"title":8,"language":10,"library":11,"_id":55},{"_path":106,"title":107,"language":10,"library":11,"_id":108},"/guides/java/okhttp/loading-javascript-from-a-string","Loading JavaScript from a string","content:guides:java:okhttp:loading-javascript-from-a-string.md",{"_path":110,"title":111,"language":10,"library":11,"_id":112},"/guides/java/okhttp/loading-javascript-from-a-url","Loading JavaScript from a URL","content:guides:java:okhttp:loading-javascript-from-a-url.md",{"_path":114,"title":115,"language":10,"library":11,"_id":116},"/guides/java/okhttp/protecting-the-generated-pdf","Protecting the generated PDF","content:guides:java:okhttp:protecting-the-generated-pdf.md",{"_path":118,"title":119,"language":10,"library":11,"_id":120},"/guides/java/okhttp/receive-a-webhook-event","Receive a webhook event","content:guides:java:okhttp:receive-a-webhook-event.md",{"_path":122,"title":123,"language":10,"library":11,"_id":124},"/guides/java/okhttp/save-your-pdf-online-and-get-back-a-url","Save your PDF online and get back a URL","content:guides:java:okhttp:save-your-pdf-online-and-get-back-a-url.md",{"_path":126,"title":127,"language":10,"library":11,"_id":128},"/guides/java/okhttp/save-your-pdf-to-your-amazon-s3-bucket","Save your PDF to your Amazon S3 bucket","content:guides:java:okhttp:save-your-pdf-to-your-amazon-s3-bucket.md",{"_path":130,"title":131,"language":10,"library":11,"_id":132},"/guides/java/okhttp/send-custom-http-headers","Send custom HTTP headers","content:guides:java:okhttp:send-custom-http-headers.md",{"_path":134,"title":135,"language":10,"library":11,"_id":136},"/guides/java/okhttp/using-cookies","Using cookies","content:guides:java:okhttp:using-cookies.md",{"_path":138,"title":139,"language":10,"library":11,"_id":140},"/guides/java/okhttp/waiting-for-a-custom-element-to-be-ready","Waiting for a custom element to be ready","content:guides:java:okhttp:waiting-for-a-custom-element-to-be-ready.md",1780405109102]