[{"data":1,"prerenderedAt":137},["Reactive",2],{"/guides/go/nethttp/convert-html-to-pdf-from-a-url":3,"related:qwRVR7MbQB":55},{"_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":17,"body":18,"_type":50,"_id":51,"_source":52,"_file":53,"_extension":54},"/guides/go/nethttp/convert-html-to-pdf-from-a-url","nethttp",false,"","Convert an HTML document to PDF from a URL","Learn how to easily convert HTML documents to PDF from a URL using Go and the Net/HTTP library. This how-to guide offers clear Go code examples that show developers how to implement this using the PDFShift API.","Go","Net/HTTP","source","pdf",[15,16],"convert-html-to-pdf-from-raw-html","avoid-conversion-on-error",true,{"type":19,"children":20,"toc":47},"root",[21,29,42],{"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 convert HTML documents to PDFs using PDFShift's API. This is a straightforward process that can be accomplished with just a few lines of Go code.",{"type":22,"tag":30,"props":31,"children":36},"pre",{"className":32,"code":34,"language":35,"meta":7},[33],"language-go","package main\n\nimport (\n    \"bytes\"\n    \"encoding/json\"\n    \"fmt\"\n    \"io/ioutil\"\n    \"net/http\"\n)\n\nfunc main() {\n    // You can get an API key at https://pdfshift.io\n    apiKey := \"sk_xxxxxxxxxxxx\"\n\n    params := map[string]interface{}{\n        \"source\":            \"https://www.exemple.com\",\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",[37],{"type":22,"tag":38,"props":39,"children":40},"code",{"__ignoreMap":7},[41],{"type":27,"value":34},{"type":22,"tag":23,"props":43,"children":44},{},[45],{"type":27,"value":46},"With this Go script, you can effortlessly convert HTML documents from a URL to PDF files using PDFShift's API. Happy coding!",{"title":7,"searchDepth":48,"depth":48,"links":49},2,[],"markdown","content:guides:go:nethttp:convert-html-to-pdf-from-a-url.md","content","guides/go/nethttp/convert-html-to-pdf-from-a-url.md","md",[56,60,64,68,72,76,77,81,85,89,93,97,101,105,109,113,117,121,125,129,133],{"_path":57,"title":58,"language":10,"library":11,"_id":59},"/guides/go/nethttp/accessing-secured-pages","Accessing secured pages","content:guides:go:nethttp:accessing-secured-pages.md",{"_path":61,"title":62,"language":10,"library":11,"_id":63},"/guides/go/nethttp/adding-a-custom-header-or-footer","Adding a custom header or footer","content:guides:go:nethttp:adding-a-custom-header-or-footer.md",{"_path":65,"title":66,"language":10,"library":11,"_id":67},"/guides/go/nethttp/adding-a-text-watermark","Adding a text watermark","content:guides:go:nethttp:adding-a-text-watermark.md",{"_path":69,"title":70,"language":10,"library":11,"_id":71},"/guides/go/nethttp/adding-an-image-watermark","Adding an image watermark","content:guides:go:nethttp:adding-an-image-watermark.md",{"_path":73,"title":74,"language":10,"library":11,"_id":75},"/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":4,"title":8,"language":10,"library":11,"_id":51},{"_path":78,"title":79,"language":10,"library":11,"_id":80},"/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":82,"title":83,"language":10,"library":11,"_id":84},"/guides/go/nethttp/define-a-time-limit","Define a time limit","content:guides:go:nethttp:define-a-time-limit.md",{"_path":86,"title":87,"language":10,"library":11,"_id":88},"/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":90,"title":91,"language":10,"library":11,"_id":92},"/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":94,"title":95,"language":10,"library":11,"_id":96},"/guides/go/nethttp/loading-css-from-a-string","Loading CSS from a string","content:guides:go:nethttp:loading-css-from-a-string.md",{"_path":98,"title":99,"language":10,"library":11,"_id":100},"/guides/go/nethttp/loading-css-from-a-url","Loading CSS from a URL","content:guides:go:nethttp:loading-css-from-a-url.md",{"_path":102,"title":103,"language":10,"library":11,"_id":104},"/guides/go/nethttp/loading-javascript-from-a-string","Loading Javascript from a string","content:guides:go:nethttp:loading-javascript-from-a-string.md",{"_path":106,"title":107,"language":10,"library":11,"_id":108},"/guides/go/nethttp/loading-javascript-from-a-url","Loading Javascript from a URL","content:guides:go:nethttp:loading-javascript-from-a-url.md",{"_path":110,"title":111,"language":10,"library":11,"_id":112},"/guides/go/nethttp/protecting-the-generated-pdf","Protecting the generated PDF","content:guides:go:nethttp:protecting-the-generated-pdf.md",{"_path":114,"title":115,"language":10,"library":11,"_id":116},"/guides/go/nethttp/receive-a-webhook-event","Receive a webhook event","content:guides:go:nethttp:receive-a-webhook-event.md",{"_path":118,"title":119,"language":10,"library":11,"_id":120},"/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":122,"title":123,"language":10,"library":11,"_id":124},"/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":126,"title":127,"language":10,"library":11,"_id":128},"/guides/go/nethttp/send-custom-http-headers","Send custom HTTP headers","content:guides:go:nethttp:send-custom-http-headers.md",{"_path":130,"title":131,"language":10,"library":11,"_id":132},"/guides/go/nethttp/using-cookies","Using cookies to convert HTML documents to PDF","content:guides:go:nethttp:using-cookies.md",{"_path":134,"title":135,"language":10,"library":11,"_id":136},"/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",1785426824576]