简体   繁体   中英

What do I have to do in order to print Blob data types (PDF) retrieved from a webserver in Node?

I don't understand how to handle Blob-Files. I have an Electron application where I retrieve a pdf from a webserver. I want to silently print the pdf retrieved from the webserver using the printer name stored in printer . I tried different solutions including different node modules but it does not work and I am not quite getting what to do. I would appreciate your help!

axios.get("getPdf", {responseType: "blob",headers: {'Accept': 'application/pdf'}).then(res => {
    let printer = printer_settings["printFormatX"];
    // I am not quite sure what to do now
    // I used different modules like 'pdf-to-printer' but it is not working like expected
    // I am generally unsure what to do with the retrieved pdf in order to print it
})

I solved it this way: I create a temporary file, then write the response data to it in order to then print it with the module pdf-to-printer

axios.get("getPdf", {responseType: "arraybuffer",headers: {'Accept': 'application/pdf'}).then(res => {
    let printer = printer_settings["printFormatX"];
    let tmpFile = __dirname + "/tmp_pdf.pdf"
    fs.writeFileSync(tmpFile, res.data, {encoding: "binary"});
    print(tmpFile, res.data, {printer: printer});
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM