Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To convert a raw HTML string to PDF in Go, send it as an index.html document to a browser-backed renderer such as Gotenberg’s Chromium HTML endpoint. With Gotenberg’s Go client, create the document using document.FromString, build an HTML request, then send it and copy the returned PDF body to a file or HTTP response. This keeps browser rendering out of your Go process while supporting HTML, CSS, and assets through a separate service.

Choose the right conversion path

Use Gotenberg’s HTML conversion route when your input is an HTML string, file, or reader that your Go application can provide as a document. It accepts a multipart upload with a required index.html file and uses Headless Chromium to produce the PDF. See the Gotenberg HTML conversion documentation.

If you need to render a live page by URL—particularly one whose content is created by JavaScript—use Gotenberg’s separate URL conversion route instead. That route loads a URL; it is not the same operation as uploading a raw HTML string. Review its network and security implications before exposing it to user-supplied URLs. See Gotenberg URL conversion.

Approach Rendering and input Choose it when Main consideration
Gotenberg HTML route with the Go client Separate HTTP service; Headless Chromium renders an uploaded HTML document. Your Go application has HTML content and you want a browser-backed renderer behind a service boundary. Operate the service and make sure referenced assets are supplied or reachable as intended.
Gotenberg URL route Separate HTTP service; Headless Chromium loads a page URL. The input is a live, JavaScript-rendered page or SPA. It loads a URL rather than accepting the raw HTML string as the document.
wkhtmltopdf Command-line tool or C library using Qt WebKit. Your tested workload works with its renderer and you can manage its external runtime. It is a different rendering engine; the cited sources do not establish current browser-feature parity or comparative performance. The project identifies it as LGPLv3 open source. See wkhtmltopdf.

Choose based on required CSS and JavaScript fidelity, how content and assets are supplied, process isolation, page layout controls, accessibility needs, and measured latency and concurrency in your own deployment. The available documentation does not provide a controlled performance comparison between these options.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

Convert a raw HTML string with Gotenberg’s Go client

1. Prepare a complete HTML document

Keep the HTML in a Go string, but treat it as a real document: include the doctype, character encoding, styles, and any needed assets. For production templates, use Go’s html/template package when inserting untrusted data so values are escaped in an HTML context. The conversion service renders the resulting document; it does not make unsafe template construction safe.

When the document references CSS, images, or fonts, use paths that the renderer can resolve. Gotenberg recommends relative paths when uploading HTML and its assets; ensure those assets are included or otherwise available to the service. A document that renders in a browser on your laptop may still omit assets if the conversion service cannot access them.

2. Create and send the conversion request

Run a Gotenberg service separately and configure the Go client to reach it. The precise client initialization and method signatures depend on the versions you pin; use the Gotenberg Go client documentation matching your selected version. The core documented flow is:

index, err := document.FromString("index.html", rawHTML)
if err != nil {
    return err
}

req := gotenberg.NewHTMLRequest(index)
resp, err := client.Send(req)
if err != nil {
    return err
}
defer resp.Body.Close()

// Copy resp.Body to an HTTP response or destination file.

document.FromString gives the HTML string the filename index.html, which is the required upload name for the HTML route. The client also documents document creation from bytes, a file path, or a reader. It provides both Send and Store workflows; use the former when your application needs to handle the response, or the documented storage flow when the service should store the result.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

3. Handle output and errors in your application

The sketch above shows the conversion flow, not a complete server or command-line program: initialize the client, select a destination, and apply an application-appropriate timeout and error handling. For an HTTP handler, copy the successful PDF response body to the client response only after handling conversion errors. For a file destination, create and close the file reliably and check copy and close errors. Avoid buffering large PDFs in memory without a reason; streaming the response body to its destination limits additional memory use.

Check the response status and content before treating it as a PDF. Do not return a partially written file as a successful conversion if the request fails or the body copy is interrupted. Configure timeouts at the client and service boundary to fit the size and loading behavior of the documents you actually convert.

Set page layout and rendering behavior

Gotenberg’s Chromium conversion request supports settings for paper dimensions, margins, orientation, scaling, print backgrounds, headers and footers, and whether CSS page size takes precedence. These choices affect pagination and visual output; consult the Go client documentation and the versioned Chromium module documentation for the exact option names and defaults for your server and client versions.

  • Paper and margins: Specify a page size and margins that suit the document rather than relying on defaults whose values may differ by version or configuration.
  • Orientation and scale: Use landscape for genuinely wide layouts, and verify scaling does not shrink text or clip content.
  • Print backgrounds: Enable background printing when colors or background images are part of the intended design.
  • CSS page size: Decide whether the document’s CSS page settings should govern the output when they conflict with request-level dimensions.
  • Headers and footers: Configure these deliberately if the PDF needs page numbers or repeated labels; check that they do not overlap document content.

For pages that load content dynamically or depend on external resources, Gotenberg provides wait controls and options related to failed resource loads and console exceptions. Configure waits to match the page’s behavior rather than assuming a particular network-idle state is always correct. Decide whether missing resources or page exceptions should fail the job, and test with representative documents.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Or skip the browser setup

If your goal is a screenshot rather than a paginated PDF, ScreenshotNeo offers a one-request website screenshot API. It accepts a URL and can return PNG, JPEG, WebP, or PDF. It is not a replacement for uploading and laying out an arbitrary raw HTML string: the API’s documented request takes a URL.

With a ScreenshotNeo API key, the cURL example is:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for request options and output configuration. ScreenshotNeo’s cleanup accepts cookie or consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each of those steps can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and responses identify the page verdict and billing status in headers. Its MCP server exposes take_screenshot, get_page_info, and capture_pdf for Claude, Cursor, and other MCP clients. The Free plan includes 1,000 screenshots per month without a card; paid plans start at $5 for 3,000 shots.

Sign up for ScreenshotNeo’s free plan to try 1,000 screenshots a month with no card.

Troubleshoot common conversion problems

The PDF is missing images, fonts, or styles

Check that each referenced asset is available to the Gotenberg service, not merely to the Go application or a developer’s browser. Use relative paths as recommended for uploaded HTML and assets, and include the required files in the conversion request or make them reachable through the intended setup. Verify the paths and permissions from the renderer’s point of view.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Content is blank or incomplete

For a raw HTML upload, confirm that the submitted document is complete and named index.html. If content is inserted after initial page load, configure the appropriate wait behavior and confirm the service can reach any remote resources. If the page is actually a live URL or JavaScript-rendered SPA, use the URL route rather than treating it as a static HTML string.

Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

Pages break, clip, or look different from the browser

Check paper dimensions, margins, orientation, scaling, print-background behavior, and CSS page-size precedence. Inspect print-specific CSS and test long tables, images, and page breaks. The HTML-to-PDF result is produced by Headless Chromium, so validate the actual PDF output rather than assuming screen rendering and print layout are identical.

The request fails or takes too long

Separate connection failures from rendering failures in your application’s error handling. Verify the client can reach the Gotenberg service and that the service is running the version expected by your client. For slow documents, inspect resource loading and dynamic-content waits, then set timeouts based on representative workload measurements. The cited sources do not establish a universal conversion time or throughput.

Resource errors or browser exceptions do not stop the job

Review Gotenberg’s controls for failed resource loads and console exceptions, and configure them according to your tolerance for incomplete documents. A permissive policy may produce a PDF despite a missing asset; a stricter policy can make the underlying issue visible as a failed conversion.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Reliability, security, and cost considerations

A browser-backed converter is a separate service to deploy, configure, monitor, and update. That boundary can be useful when multiple applications need the same conversion capability, but it also means service availability, network reachability, resource access, and queueing matter to your application. The documentation cited here does not supply a comparative reliability or performance guarantee; measure conversion latency, failure rates, and concurrency with your own HTML and assets.

Consider the security boundary whenever HTML or URLs are user-controlled. Apply your application’s input validation and access controls, be cautious about permitting arbitrary resource access, and avoid forwarding secrets in a document that may be rendered or logged by another service. URL conversion has different network behavior from uploading a local HTML document; restrict what URLs the service is allowed to load according to your deployment’s threat model.

Costs depend on how you host or operate the selected renderer and on your real usage; the cited product documentation does not establish a universal price or cost-per-document comparison. Pin compatible client and server versions, and check the corresponding versioned documentation before relying on API signatures or default layout values.

Frequently Asked Questions

Can I convert HTML assembled from a Go template?

Yes. Render the template to a string, then submit that string as the HTML document. Use html/template for contextual escaping when template data may be untrusted.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Should I use Gotenberg’s HTML route or URL route?

Use the HTML route for a string or document your application uploads. Use the URL route when the input is a live page that the renderer must load.

Is wkhtmltopdf equivalent to Chromium for PDF rendering?

No. The documented engines differ: Gotenberg’s HTML route uses Headless Chromium, while wkhtmltopdf uses Qt WebKit. Validate the renderer against your required CSS and JavaScript behavior.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.