e-Ra
e-Ra

Reputation: 3

Print PDF from rust using pdfium and winapi

I am trying to print a PDF in rust using pdfium and winapi.
The code runs through without errors and the debug output also looks good.
In the printer queue you can briefly see the job flashing but no document is printed.

I have tested once with a PDF printer ("Microsoft Print to PDF") and a normal printer.
The PDF printer creates the output file, but it has 0 bytes and cannot be opened.

I have the impression that it is due to pdfium or the FPDF_RenderPage method, but I have no idea how to debug this. Does anyone have an idea what the problem could be?

[repr(C)]
#[derive(Clone, Copy)]
struct FPDF_LIBRARY_CONFIG {
    version: i32,
    m_pUserFontPaths: *const c_char,
    m_pIsolate: *const c_void,
    m_v8EmbedderSlot: u32,
}

#[cfg(target_os = "windows")]
#[link(name = "pdfium")]
extern "C" {
    fn FPDF_InitLibraryWithConfig(config: *const FPDF_LIBRARY_CONFIG) -> c_void;

    fn FPDF_DestroyLibrary() -> c_void;

    fn FPDF_LoadMemDocument(
        file_content: *const c_void,
        content_size: i32,
        password: *const c_char,
    ) -> *const c_void;

    fn FPDF_LoadPage(document: *const c_void, page_index: i32) -> *const c_void;

    fn FPDF_GetPageWidthF(page: *const c_void) -> c_float;

    fn FPDF_GetPageHeightF(page: *const c_void) -> c_float;

    fn FPDF_RenderPage(
        hdc: HDC,
        page: *const c_void,
        start_x: i32,
        start_y: i32,
        size_x: i32,
        size_y: i32,
        rotate: i32,
        flags: i32,
    ) -> c_void;
}
pub fn print(file: &[u8]) -> anyhow::Result<()> {
    use std::ptr::null;

    use windows::{
        core::{s, PCSTR, PSTR},
        Win32::{
            Foundation::HANDLE,
            Graphics::{
                Gdi::{CreateDCA, DeleteDC},
                Printing::{
                    ClosePrinter, EndDocPrinter, EndPagePrinter, OpenPrinterA, StartDocPrinterA, StartPagePrinter, DOC_INFO_1A
                },
            },
        },
    };

    unsafe {
        let hdc = CreateDCA(PCSTR::null(), s!("Microsoft Print to PDF"), None, None);
        println!("create dca: {} {:?}", hdc.is_invalid(), hdc);

        let mut printer_handle = HANDLE::default();
        
        let data_type = "XPS_PASS\0"; // also tried "RAW" and "Text"

        let resp_open_printer = OpenPrinterA(s!("Microsoft Print to PDF"), &mut printer_handle, None);
        println!("open printer: {} {:?}", resp_open_printer.is_ok(), printer_handle);

        let doc_name = "Test Document\0";
        let doc_Info = DOC_INFO_1A {
            pDocName: PSTR::from_raw(doc_name.as_ptr().cast_mut()),
            pOutputFile: PSTR::null(),
            pDatatype: PSTR::from_raw(data_type.as_ptr().cast_mut()),
        };

        let resp_start_doc_printer = StartDocPrinterA(printer_handle, 1, &doc_Info);
        println!("start doc printer: {}", resp_start_doc_printer);

        let resp_start_page_printer = StartPagePrinter(printer_handle);
        println!("start page printer: {}", resp_start_page_printer.as_bool());

        let config = FPDF_LIBRARY_CONFIG {
            version: 2,
            m_pUserFontPaths: null(),
            m_pIsolate: null(),
            m_v8EmbedderSlot: 0,
        };

        FPDF_InitLibraryWithConfig(&config);

        let doc = FPDF_LoadMemDocument(file.as_ptr() as *const c_void, file.bytes().count() as i32, null());
        println!("load mem doc: {:?} {}", doc, file.bytes().count() as i32);

        let page = FPDF_LoadPage(doc, 0);
        println!("load page: {:?}", page);

        let resp_page_width = FPDF_GetPageWidthF(page);
        println!("page width: {}", resp_page_width);

        let resp_page_height = FPDF_GetPageHeightF(page);
        println!("page height: {}", resp_page_height);

        FPDF_RenderPage(hdc, page, 0, 0, resp_page_width as i32, resp_page_height as i32, 0, 0x800);

        let resp_end_page_printer = EndPagePrinter(printer_handle);
        println!("end page printer: {}", resp_end_page_printer.as_bool());

        let resp_end_doc_printer = EndDocPrinter(printer_handle);
        println!("end doc printer: {}", resp_end_doc_printer.as_bool());

        let resp_close_printer = ClosePrinter(printer_handle);
        println!("close printer: {}", resp_close_printer.is_ok());

        let resp_delete_dc = DeleteDC(hdc);
        println!("delete dc: {}", resp_delete_dc.as_bool());

        FPDF_DestroyLibrary();
    }
    
    ...
}

Upvotes: 0

Views: 130

Answers (0)

Related Questions