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?

  • FFI
[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;
}
  • Printing
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"; // 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";
        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();
    }
    
    ...
}
  • Debug output

    create dca: false HDC(-551480207) // false is good
    open printer: true HANDLE(1372067071760)
    start doc printer: 73
    start page printer: true
    load mem doc: 0x13f780cc190 4953
    load page: 0x13f758f9730
    page width: 297.64 // correct width
    page height: 575.43005 // correct height
    end page printer: true
    end doc printer: true
    close printer: true
    delete dc: true

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật