-
I'm trying to open a new window with a pdf file content using the following code:
I really appreciate any help |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
you should render the pdf by yourself , tauri wrapped the webview, but it's not a complete browser, maybe you can take a look at |
Beta Was this translation helpful? Give feedback.
-
You should render the PDF by yourself, Tauri wrapped the webview, but it's not a complete browser, Maybe you can take a look at |
Beta Was this translation helpful? Give feedback.
-
In case anyone needs a newer version of this, this works (as of tauri 2.3.1): # Cargo.toml
tauri = { version = "2", features = ["unstable", "webview-data-url"] } import { error, info } from "@tauri-apps/plugin-log";
import { WebviewWindow } from "@tauri-apps/api/webviewWindow";
import { BaseDirectory, readFile } from "@tauri-apps/plugin-fs";
export async function openInvoicePDF(path: string, contentType: string = "application/pdf") {
const content = await readFile(path, {
baseDir: BaseDirectory.AppData,
});
const base64 = await bufferToBase64(content);
const invoiceWindow = new WebviewWindow(`invoice`, { // maybe use uniq label here
url: `data:${contentType};base64,${base64}`,
x: 0,
y: 0,
width: 800,
height: 600,
});
invoiceWindow.once("tauri://created", () => {
info(`Opened window with invoice ${upload.id}`);
});
invoiceWindow.once("tauri://error", (e) => {
error(`Error opening invoice window ${upload.id}: ${JSON.stringify(e)}`);
});
}
// from https://stackoverflow.com/a/66046176
async function bufferToBase64(buffer: Uint8Array) {
const base64url = await new Promise<string>((r) => {
const reader = new FileReader();
reader.onload = () => r(reader.result as string);
reader.readAsDataURL(new Blob([buffer]));
});
return base64url.slice(base64url.indexOf(",") + 1);
} And add the necessary capabilities: "core:webview:default",
"core:webview:allow-create-webview-window",
"core:webview:allow-create-webview" |
Beta Was this translation helpful? Give feedback.
@newproplus, thanks for you response.
I solve this problem doing this: