#Automatically share pdf on mobiles

1 messages · Page 1 of 1 (latest)

outer sedge
timber adder
outer sedge
#

awesome! I'll give that a shot

#
export let loader = async ({ request, params }: LoaderArgs) => {
  // you can get any data you need to generate the PDF inside the loader
  // however you want, e.g. fetch an API or query a DB or read the FS
  //   let data = await getDataForThePDFSomehow({ request, params });
  let data = "hello world";

  // render the PDF as a stream so you do it async
  let stream = await renderToStream(<PDFDocument data={data} />);

  // and transform it to a Buffer to send in the Response
  let body: Buffer = await new Promise((resolve, reject) => {
    let buffers: Uint8Array[] = [];
    stream.on("data", (data) => {
      buffers.push(data);
    });
    stream.on("end", () => {
      resolve(Buffer.concat(buffers));
    });
    stream.on("error", reject);
  });

  // finally create the Response with the correct Content-Type header for
  // a PDF
  let headers = new Headers({ "Content-Type": "application/pdf" });
  return new Response(body, { status: 200, headers });
};

I'm using react-pdf so where would the api be applied in this loader?

#

or would it be on the <Link>

timber adder
#

It's a browser-based API, so you'll have to run it on the browser.

#

So yeah, its' on the link. I'm afraid I've never used the Web Share API, so I can't share details.