Closed ulvido closed 1 year ago
@ulvido you can't, it would need modifications to the user code in this example
Fixed with https://github.com/fschutt/printpdf/pull/127
Thank you. I can confirm that problem resolved. I have notations:
cargo add printpdf
because at the moment crates.io version is outdated.serde_bytes
for the payload and send it to the javascript side.#...
[dependencies]
wasm-bindgen = "0.2.63"
serde = { version = "1.0.160", features = ["derive"] }
serde-wasm-bindgen = "0.5.0"
serde_bytes = "0.11.9"
printpdf = { git = "https://github.com/fschutt/printpdf.git" }
use printpdf::*;
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;
mod utils;
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[wasm_bindgen]
extern "C" {
fn alert(s: &str);
}
#[wasm_bindgen]
pub fn greet() {
alert("Hello, printpdf!");
}
#[derive(Deserialize, Serialize)]
struct Message {
#[serde(with = "serde_bytes")]
payload: Vec<u8>,
}
#[wasm_bindgen]
pub fn create(title: &str, text: &str) -> JsValue {
let (doc, page1, layer1) = PdfDocument::new(title, Mm(247.0), Mm(210.0), "Layer 1");
let current_layer = doc.get_page(page1).get_layer(layer1);
// use built-in font
let font = doc.add_builtin_font(BuiltinFont::TimesRoman).unwrap();
current_layer.begin_text_section();
current_layer.use_text(text, 22.0, Mm(30.0), Mm(30.0), &font);
current_layer.end_text_section();
let bytes: Vec<u8> = doc.save_to_bytes().unwrap();
let m = Message { payload: bytes };
serde_wasm_bindgen::to_value(&m).unwrap()
}
<script lang="ts">
let pdf_title;
let pdf_text;
import init, { greet, create } from "pdf-maker"; // my wasm create name
init().then((module) => {
console.log(module);
// greet();
});
const createpdf = () => {
const blob = new Blob([create(pdf_title, pdf_text).payload], {
type: "application/pdf",
});
const blobURL = URL.createObjectURL(blob);
window.open(blobURL);
};
</script>
<main>
<div class="card">
<input
bind:value={pdf_title}
placeholder="PDF Title"
type="text"
/>
</div>
<div class="card">
<textarea
bind:value={pdf_text}
placeholder="PDF Text"
cols="30"
rows="10"
/>
</div>
<div class="card">
<button on:click={createpdf }>Create PDF</button>
</div>
</main>
Hi! I am new to Rust. I want to show wasm generated pdf with live changes in browser.
How can I solve this shared reference problem? Is there a trait implementaion missing? How can I
clone
ortake ownership
of the doc? thank you.Purpose:
to convert pdf to bytes array and sent to js side.
Code
Build Command
Error