Displaying pdf from arraybuffer

Displaying pdf from arraybuffer

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Displaying PDF from ArrayBuffer in Browser

When generating a PDF on the server side using Laravel's dompdf wrapper, the typical approach is to return the binary stream directly. However, modern frontend applications often require handling these responses differently, specifically by requesting an arraybuffer via AJAX. This shift changes how the browser interprets the incoming data and how you must construct the final resource object for rendering.

In your current setup, Laravel's $pdf->stream() method generates a response containing binary PDF data. When your JavaScript client requests this endpoint with responseType: 'arraybuffer', the framework correctly returns the raw bytes as an array of numbers (integers). The issue arises in how you are reconstructing the file object and attaching it to the <object> element.

The core problem lies in this line:

var pdfurl = window.URL.createObjectURL(blob)+"#view=FitW";
$("#pdfviewer").attr("data",pdfurl);

You are creating a valid Blob URL using window.URL.createObjectURL(), which is excellent for holding binary data in memory. However, you are appending the string "#view=FitW" directly to that URL. While some browsers might parse this as a fragment identifier, others may treat it as part of the path or ignore it entirely depending on the rendering engine's strictness regarding PDF parameters. More critically, your HTML anchor points to /files/sample.pdf initially, but your JavaScript updates the data attribute with the new Blob URL. This mismatch suggests the initial HTML was a placeholder, which is fine for prototyping, but the transition must be seamless.

A more robust pattern involves passing the blob data directly to the <object> tag or updating the source correctly without relying on string concatenation for view parameters that the browser does not natively support in the data attribute context. The PDF viewer embedded in the DOM often struggles when a Blob URL is constructed with arbitrary fragments appended manually.

To fix the "Failed to load PDF document" error, you should construct the object URL cleanly and ensure the fragment identifier is handled via CSS or specific PDF viewer libraries rather than string concatenation on the data attribute. Here is the corrected approach:

  1. Construct the Blob correctly: Ensure the response.data from your AJAX call is treated as binary. In jQuery, when using responseType: 'arraybuffer', the response is indeed an ArrayBuffer. You must convert this to a Blob with the correct MIME type.
  2. Create the URL: Use window.URL.createObjectURL(blob) to generate a local reference.
  3. Update the DOM: Set the data attribute of your <object> tag to this URL. Do not append #view=FitW directly to the URL string if you want standard browser rendering. If you require specific zooming behavior, consider using a library like PDF.js which accepts these parameters via its API rather than the HTML data attribute.
$.ajax({
    type: "GET",
    url: "/display",
    responseType: 'arraybuffer'
}).done(function(response) {
    // response is an ArrayBuffer from jQuery
    var blob = new Blob([response], {type: 'application/pdf'});
    var pdfUrl = window.URL.createObjectURL(blob);

    // Update the object tag directly without string concatenation of fragments
    $("#pdfviewer").attr("data", pdfUrl).attr("type", "application/pdf");
    
    // Optional: If you need specific view settings, use a library like PDF.js
    // which handles zooming via JavaScript API rather than HTML attributes.
});

Why String Concatenation Fails with Blob URLs

The #view=FitW fragment is standard in some legacy PDF viewers to force a "fit width" layout. However, when using a dynamically generated blob: URL, the browser's native PDF plugin or renderer may not recognize fragments on the URL itself. The data attribute expects a direct reference to the resource. By appending the string, you risk creating a malformed URL that the rendering engine cannot resolve to a valid document stream.

If you strictly require "Fit Width" behavior without external libraries, consider injecting CSS rules into the <object> element or using a wrapper library like pdfjs-dist. These tools parse the Blob data and apply zoom settings programmatically, bypassing the limitations of the HTML5 <object> tag's fragment handling.

Server-Side Considerations

On the Laravel side, ensure that your route returns the correct Content-Type header. While dompdf->stream() handles this automatically in most cases, explicitly setting the response headers in your controller can prevent caching issues or MIME type mismatches:

return $pdf->stream('document.pdf', [
    'mimetype' => 'application/pdf',
    'inline' => true, // Ensures the file is displayed rather than downloaded
]);

By aligning the client-side reconstruction of the binary data with the server's explicit MIME type declaration, you eliminate the ambiguity that causes the loader to fail. The browser needs to know it is receiving a PDF stream, not arbitrary text or HTML, regardless of how the URL is constructed.

Our network