我正在try 在Tauri应用程序中从HTML默默创建PDF.据我所知,Tauri目前不支持这一点,因此我需要直接与平台特定的接口通信.

我发现对于Windows,我需要获取窗口的ICoreWebView2_7,因为它有一个PrintToPdf方法.我该怎么办?(或者还有其他更好的方法来解决这个问题吗?)

我设法像这样从webview2_com_sys获取了ICoreWebView2.

let _ = main_window.with_webview(|webview| {
    #[cfg(windows)] 
    unsafe {
        let corewebview = webview.controller().CoreWebView2().expect("");
    }
}

但print ToPDF方法为ICoreWebView2_7.那么如何从ICoreWebView2中得到ICoreWebView2_7呢?

推荐答案

这仍然相当复杂,需要您指定这两个依赖项(它们应该已经隐含在您的项目中,因为它们由tauri本身使用).我使用了Tauri 1.6.1的匹配版本,这些版本目前已经相当旧了:

webview2-com = "0.19.1"
windows = "0.39.0"

那么您可以直接投射(在这种情况下是ICoreWebView2_10):

#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use tauri::Manager;
use webview2_com::{Microsoft::Web::WebView2::Win32::*, *};
use windows::core::Interface;

fn main() {
    tauri::Builder::default()
        .setup(|app| {
            let main_window = app.get_window("main").unwrap();
            main_window
                .with_webview(|webview| {
                    #[cfg(windows)]
                    unsafe {
                        let Ok(webview) = webview
                            .controller()
                            .CoreWebView2()
                            .unwrap()
                            .cast::<ICoreWebView2_10>() // This is where the magic happens.
                        else {
                            panic!("Failed to cast PlatformWebview.");
                        };

                        //webview.PrintToPdf().expect("Failed to print to pdf.");
                    }
                })
                .expect("Failed to get webview.");
            Ok(())
        })
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

Rust相关问答推荐

移植带有可变borrow 的C代码-卸载期间错误(nappgui示例)

在‘await’点上使用‘std::同步::Mutex’是否总是会导致僵局?

收集RangeInclusive T到Vec T<><>

通过解引用将值移出Box(以及它被脱糖到什么地方)?

为什么reqwest以文本形式下载二进制文件?

获取与父字符串相关的&;str的原始片段

为什么HashMap::get和HashMap::entry使用不同类型的密钥?

&'a T 是否意味着 T: 'a?

结果流到 Vec 的结果:如何避免多个into_iter和collect?

为什么 js_sys Promise::new 需要 FnMut?

write_buffer 不写入缓冲区而是输出零 WGPU

注释闭包参数强调使用高阶排定特征界限

Rust 中的 Option as_ref 和 as_deref 有什么不同

如何在 Rust 的 Hyper 异步闭包中从外部范围正确读取字符串值

从 Rust 中的 if/else 中的引用创建 MappedRwLockWriteGuard

从现有系列和 map 值创建新系列

试图理解 Rust 中的可变闭包

为什么 match 语句对引用类型比函数参数更挑剔?

Rust 为什么 (u32, u32) 的枚举变体的大小小于 (u64)?

如何在宏中的多个参数上编写嵌套循环?