Load a PDF from a Stream

It’s possible to load a PDF from a stream. PSPDFKit for Web can load a document from an ArrayBuffer, so you’ll need to convert your stream into an ArrayBuffer that can be passed in PSPDFKit.Configuration#pdf. Here’s how to do this:

// Replace "URL" with your stream URL.
fetch(URL)
  // Retrieve its body as `ReadableStream`.
  .then((response) => response.body)
  .then((rs) => {
    const reader = rs.getReader();

    return new ReadableStream({
      async start(controller) {
        while (true) {
          const { done, value } = await reader.read();

          // When no more data needs to be consumed, break the reading.
          if (done) {
            break;
          }

          // Enqueue the next data chunk into our target stream.
          controller.enqueue(value);
        }

        // Close the stream.
        controller.close();
        reader.releaseLock();
      }
    });
  })
  // Create a new response out of the stream.
  .then((rs) => new Response(rs))
  // Create an object URL for the response.
  .then((response) => response.arrayBuffer())
  .then((arrayBuffer) => {
    PSPDFKit.load({
      document: arrayBuffer
    }).then((instance) => {
      // Your PSPDFKit code here.
    });
  });

This snippet was adapted from an MDN example.