jussiniinikoski / wasm-pdf

Generate PDF files with JavaScript and WASM (WebAssembly)
Apache License 2.0
480 stars 52 forks source link

How this repo works without return anything #37

Closed axetroy closed 1 year ago

axetroy commented 1 year ago

https://github.com/jussiniinikoski/wasm-pdf/blob/467eef02a67250c61956d94c58dc2419cfe19ecc/src/lib.rs#L18C5-L29

#[wasm_bindgen]
pub fn run(json: &JsValue) -> Result<(), JsValue> {
    // output panics to console.error
    console_error_panic_hook::set_once();
    let js_doc = get_js_doc(&json)?;
    let bytes = match create(&js_doc) {
        Ok(b) => b,
        Err(s) => return Err(JsValue::from_str(s)),
    };
    generate_file(&bytes);
    Ok(())
}

The run function return nothing, and How could I get the PDF binary or URL?

jussiniinikoski commented 1 year ago

This function runs the generate_file or "generatePDF" function on JS side with created bytes. It's easier to customize this way.

axetroy commented 1 year ago

@jussiniinikoski Thanks for your response. and I finally found it.

    let generatePDF = (data) => {
      const blob = new Blob([data], {
        type: 'application/pdf'
      });
      if (pdfFileBlobURL !== null) {
        URL.revokeObjectURL(pdfFileBlobURL);
      }
      pdfFileBlobURL = URL.createObjectURL(blob);
      // for debugging purposes, open another window
      //window.open(pdfFileBlobURL, "_blank");
      window.location.href = pdfFileBlobURL;
    }

To be honest, it's not obvious that such an interface.

I am going to transform it and return bytes or Blob/File directly