tojocky / node-printer

Native node.js printer
1.54k stars 443 forks source link

Print PDF from Windows #29

Open rclai opened 10 years ago

rclai commented 10 years ago

Here's the link to the PDF:

https://drive.google.com/file/d/0B4xBPiAxQ7h5STZPWjlGREJKd0k/view?usp=sharing

We used MS Word to create a PDF and that printed fine. The above PDF was generated using a PHP library called FPDF:

I'm using this code to print it. When we attempt to print the above PDF, the printer will get the data, but it will not print it.

printer.printDirect({
    data:  fs.readFileSync('./doc.pdf')
    , printer: 'Ricoh Aficio MP C2500 PCL5c' // printer name, that does exist
    , type: 'RAW'
    , success: function (jobID)  {
        console.log("sent to printer with ID: " + jobID);
    }
    , error: function (err) {
        throw new Error(err);
    }
});
tojocky commented 10 years ago

Did you try type: 'PDF' instead of type: 'RAW'?

What OS do you use?

rclai commented 10 years ago

Yes, tried PDF, and it threw error because it is not compatible. I'm on Windows 7 Professional.

tojocky commented 10 years ago

on windows is not yet supported. You can send RAW/TEXT data only. Feel free to propose PDF support on windows. Originally this code was created to communicate with printer directly (Zebra barcode printer).

Wujerry commented 9 years ago

Any news? Or is there a way to print html using node-webkit?

tojocky commented 9 years ago

good link to start is: https://msdn.microsoft.com/en-us/library/windows/desktop/dd145226(v=vs.85).aspx

tojocky commented 9 years ago

https://msdn.microsoft.com/en-us/library/windows/desktop/dd162624(v=vs.85).aspx

tojocky commented 9 years ago

http://stackoverflow.com/questions/4565679/windows-how-to-tell-printer-to-issue-a-formfeed-during-printing

alexeyst commented 9 years ago

This link claims that a PDF can be printed with the seemingly similar approach utilised by the Windows version of node-printer: https://vishalsbsinha.wordpress.com/2014/05/06/how-to-programmatically-c-net-print-a-pdf-file-directly-to-the-printer/.

tojocky commented 9 years ago

It depends on the printer and processor format. I tried on my printer and it is not possible. The code from the link just read the file bytes and sends it to printer as RAW type.

alexeyst commented 9 years ago

That's right @tojocky. There's an open source project, part of Chromium, which is called PDFium. It seems to be able to render a PDF on a DC, which should work as explained here. What are your thoughts?

alexeyst commented 9 years ago

Here's an example of how to do this from Chromium. See RenderPDFPageToDC by that link. Do you think this might be the way to go for the module? I am actually using it in node-webkit environment, which indeed has the pdf.dll shipped with it on Windows, so some integration on that end is possible.

What are your thoughts on how this should be implemented @tojocky?

tojocky commented 9 years ago

From the code I see it transform the PDF format into EMF format. Technically it is possible, but not sure if it worth to import the whole library of PDFium. The library contains more than transforming PDF format into EMF.

alexeyst commented 9 years ago

Yeah, here is what I meant. Anyways, to print a PDF on Windows we definitely need a tool to render it, so unless this library is used, we will need to find another one.

Another approach that I came across now is rather a workaround — why don't we allow printing images on Windows, and then printing a PDF will be a 2 step process, one of which is PDF rasterization that can be done elsewhere. What do you think @tojocky?

tojocky commented 9 years ago

I did some investigation and you are right. PDFium seems to be the only reliable solution. Other solutions like imagemagick, require more dependencies (postscript) and does not work properly in the end.

I will reconsider the PDFium project to be included in node-printer or to create as a separate node package. Thanks for this.

tojocky commented 9 years ago

I have good progress. I managed to link pdfium to node/iojs and render PDF to PNG and PPM on both systems. It remains to render to EMF, BMP for windows. will upload the pdfium binding to https://github.com/tojocky/node-pdfium once I it is finished.

tojocky commented 9 years ago

Done.

Will ad here an example.

feel free to test.

pushpak commented 9 years ago

does this also support sending base64 data or a canvas image ? (windows)

tojocky commented 9 years ago

You can send buffer, why do you want to send base64? What do you mean by canvas? EMF is a canvas, by using node-pdfium can be converted from a PDF format.

alexeyst commented 9 years ago

Nice work @tojocky! Have you already tried to actually print a PDF via a combination of node-pdfium and node-printer and possibly got a working example? Also, is there a way to pass a dpi value for rendering?

Also, on a side note, I posted a feature request to NW repo too and it was confirmed that this will be supported in NW 0.13 or earlier, too: https://github.com/nwjs/nw.js/issues/3240.

tojocky commented 9 years ago

I tested on windows to save the EMF file and it does OK, I could open to see it. Regarding DPI, I didn't do yet, I've seen chromium has something on this but needs more investigation. I tried to send EMF format to printer, but still don't have the desired result, even the saved file I could open and see it. I will update here, once I have good result.

tojocky commented 9 years ago

Somebody can test this code on windows 7/8?

I have a virtual windows 10 and seems EMF is removed. Instead I have the following formats: NT EMF 1.003, NT EMF 1.006, NT EMF 1.008, NT EMF 1.008. I tried these but no luck. The generated EMF file I could open and see it.

The following piece of code:

var pdfium = require('pdfium'), // in your case require('pdfium');
    util = require('util'),
    fs = require('fs'),
    path = require('path'),
    os = require('os'),
    util = require('util'),
    printer,
    options = {
        data: fs.readFileSync(path.join(__dirname, 'pdf-sample.pdf')),
        outputFormat: process.argv[2] || 'EMF' // could be a item from pdfium.getSupportedOutputFormats();
    },
    printerType = process.argv[3] || options.outputFormat;

try {
    printer =  require('printer');
} catch (err) {
    return console.log('please install printer module `npm install printer`');
}

var supportedPrintFormats = printer.getSupportedPrintFormats();

if(supportedPrintFormats.indexOf(printerType) == -1) {
    return console.log('unsupported format ' + options.outputFormat + '. please select one of:' + util.inspect(supportedPrintFormats, {colors: true}));
}

console.log('converting PDF to ', options.outputFormat);

//async
console.log('run async');
pdfium.render(options, function(err, pages) {
    if(err) {
        return console.error('error from async convert: ', err);
    }
    console.log('start printing');
    var i, processing = 0,
        endProcessing = function() {
            --processing;
            if(processing <= 0) {
                console.log('finished');
            }
        };
    for(i = 0; i < pages.length; ++i) {
        console.log('print page ' + i);
        var page = pages[i];
        printer.printDirect({
            data: page,
            type: printerType,
            success: function(id) {
                console.log('sent to printer with ID:' + id);
                endProcessing();
            },
            error: function(err) {
                console.log('error from printer:' + err);
                endProcessing();
            }
        })
    }
})
alexeyst commented 9 years ago

I can try this later in the week @tojocky

vikrantpogula commented 9 years ago

@tojocky i am currently trying to get this to work on Windows 7 and mac and seem to be running into a lot of issues trying to install node-pdfium.

On windows : Mainly cannot find some .h files in /third_party/pdfium On Mac:

fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
make: *** [Release/obj.target/javascript/third_party/pdfium/fpdfsdk/src/javascript/app.o] Error 1

It fails during node-gyp rebuild on both platforms for me. Do you want me to give you the npm-debug.log ?

vikrantpogula commented 9 years ago

FYI : I have Visual Studio 2012 installed on windows 7

tojocky commented 9 years ago

What header files are missing?

vikrantpogula commented 9 years ago

@tojocky heres the full traceback. It says multiple times that the headers are missing

https://s3-ap-southeast-1.amazonaws.com/temp-files-dump/npm-install.txt

c:\users\vikrant pogula\desktop\node test\third_party\pdfium\core\src\fxcodec\fx_libopenjpeg\libopenjpeg20\opj_inttypes.h(36): fatal error C1083: Cannot open include file: 'inttypes.h': No such file or directory (..\..\..\third_party\pdfium\core\src\fxcodec\fx_libopenjpeg\src\fx_image.c)

Most of them seem to be this inttypes.h from fx_libopenjpeg

Let me know if you need anything else

tojocky commented 9 years ago

I tried to compile with msvs 2013, could you please retry with this compiler? On 31 Mar 2015 16:33, "Vikrant Pogula" notifications@github.com wrote:

@tojocky https://github.com/tojocky heres the full traceback. It says multiple times that the headers are missing

https://s3-ap-southeast-1.amazonaws.com/temp-files-dump/npm-install.txt

c:\users\vikrant pogula\desktop\node test\third_party\pdfium\core\src\fxcodec\fx_libopenjpeg\libopenjpeg20\opj_inttypes.h(36): fatal error C1083: Cannot open include file: 'inttypes.h': No such file or directory (......\third_party\pdfium\core\src\fxcodec\fx_libopenjpeg\src\fx_image.c)

Most of them seem to be this 'inttypes.h' from fx_libopenjpeg

Let me know if you need anything else

— Reply to this email directly or view it on GitHub https://github.com/tojocky/node-printer/issues/29#issuecomment-88135371.

vikrantpogula commented 9 years ago

downloading the compiler. Do you by chance have a direct link around for it ?

EDIT: I got it kind of run with msvs 2012, with only one error (instead of many). Missing inttypes.h. Will update you once i try 2013

vikrantpogula commented 9 years ago

with msvs 2013 on win7 i get this now.

Warning: Missing input files:
C:\Users\Virtual\Desktop\node-pdfium\build\third_party\pdfium\..\..\..\third_party\pdfium\core\src\fxcodec\libjpeg\makefile
Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
C:\Program Files\Windows Kits\8.1\DesignTime\CommonConfiguration\neutral\Windows.arm.props : error MSB4025: The project file could not be loaded. Root element is missing. [C:\Users\Virtual\Desktop\node-pdfium\build\third_party\pdfium\third_party\bigint.vcxproj]
C:\Program Files\Windows Kits\8.1\DesignTime\CommonConfiguration\neutral\Windows.arm.props : error MSB4025: The project file could not be loaded. Root element is missing. [C:\Users\Virtual\Desktop\node-pdfium\build\third_party\pdfium\fdrm.vcxproj]
C:\Program Files\Windows Kits\8.1\DesignTime\CommonConfiguration\neutral\Windows.arm.props : error MSB4025: The project file could not be loaded. Root element is missing. [C:\Users\Virtual\Desktop\node-pdfium\build\third_party\pdfium\formfiller.vcxproj]
C:\Program Files\Windows Kits\8.1\DesignTime\CommonConfiguration\neutral\Windows.arm.props : error MSB4025: The project file could not be loaded. Root element is missing. [C:\Users\Virtual\Desktop\node-pdfium\build\third_party\pdfium\fpdfapi.vcxproj]
C:\Program Files\Windows Kits\8.1\DesignTime\CommonConfiguration\neutral\Windows.arm.props : error MSB4025: The project file could not be loaded. Root element is missing. [C:\Users\Virtual\Desktop\node-pdfium\build\third_party\pdfium\fpdfdoc.vcxproj]
C:\Program Files\Windows Kits\8.1\DesignTime\CommonConfiguration\neutral\Windows.arm.props : error MSB4025: The project file could not be loaded. Root element is missing. [C:\Users\Virtual\Desktop\node-pdfium\build\third_party\pdfium\fpdftext.vcxproj]
C:\Program Files\Windows Kits\8.1\DesignTime\CommonConfiguration\neutral\Windows.arm.props : error MSB4025: The project file could not be loaded. Root element is missing. [C:\Users\Virtual\Desktop\node-pdfium\build\third_party\pdfium\third_party\freetype.vcxproj]
C:\Program Files\Windows Kits\8.1\DesignTime\CommonConfiguration\neutral\Windows.arm.props : error MSB4025: The project file could not be loaded. Root element is missing. [C:\Users\Virtual\Desktop\node-pdfium\build\fx_lpng.vcxproj]
C:\Program Files\Windows Kits\8.1\DesignTime\CommonConfiguration\neutral\Windows.arm.props : error MSB4025: The project file could not be loaded. Root element is missing. [C:\Users\Virtual\Desktop\node-pdfium\build\third_party\pdfium\fxcodec.vcxproj]
C:\Program Files\Windows Kits\8.1\DesignTime\CommonConfiguration\neutral\Windows.arm.props : error MSB4025: The project file could not be loaded. Root element is missing. [C:\Users\Virtual\Desktop\node-pdfium\build\third_party\pdfium\fxcrt.vcxproj]
C:\Program Files\Windows Kits\8.1\DesignTime\CommonConfiguration\neutral\Windows.arm.props : error MSB4025: The project file could not be loaded. Root element is missing. [C:\Users\Virtual\Desktop\node-pdfium\build\third_party\pdfium\fxedit.vcxproj]
C:\Program Files\Windows Kits\8.1\DesignTime\CommonConfiguration\neutral\Windows.arm.props : error MSB4025: The project file could not be loaded. Root element is missing. [C:\Users\Virtual\Desktop\node-pdfium\build\third_party\pdfium\fxge.vcxproj]
C:\Program Files\Windows Kits\8.1\DesignTime\CommonConfiguration\neutral\Windows.arm.props : error MSB4025: The project file could not be loaded. Root element is missing. [C:\Users\Virtual\Desktop\node-pdfium\build\third_party\pdfium\javascript.vcxproj]
C:\Program Files\Windows Kits\8.1\DesignTime\CommonConfiguration\neutral\Windows.arm.props : error MSB4025: The project file could not be loaded. Root element is missing. [C:\Users\Virtual\Desktop\node-pdfium\build\third_party\pdfium\jsapi.vcxproj]
C:\Program Files\Windows Kits\8.1\DesignTime\CommonConfiguration\neutral\Windows.arm.props : error MSB4025: The project file could not be loaded. Root element is missing. [C:\Users\Virtual\Desktop\node-pdfium\build\third_party\pdfium\third_party\pdfium_base.vcxproj]
C:\Program Files\Windows Kits\8.1\DesignTime\CommonConfiguration\neutral\Windows.arm.props : error MSB4025: The project file could not be loaded. Root element is missing. [C:\Users\Virtual\Desktop\node-pdfium\build\third_party\pdfium\pdfwindow.vcxproj]
tojocky commented 9 years ago

did you rebuild?

did you try to build with node-gyp --msvs_version=2013 or the same param for nw-gyp?

vikrantpogula commented 9 years ago

yes, this is the error i get for node-gyp rebuild --msvs_version=2013

tojocky commented 9 years ago

strange, I see Windows.arm.props. What is your arch in the end?

vikrantpogula commented 9 years ago

I was using a boxed windows 7, maybe thats they issue. Should I try it with windows 8 which i have on bootcamp ? I actually want to get it to run on windows 7

vikrantpogula commented 9 years ago

I've been unable to get it to build with a variety of combinations. Same errors.

Just hit a new one.

os_posix is not defined for condition [os_posix==1]

I will try again over the weekend.

btw thanks for maintaining this package ! its one of the most useful part of my app right now :)

tojocky commented 9 years ago

@vikrantpogula Please open an issue in https://github.com/tojocky/node-pdfium/issues to separate by windows PDF.

vikrantpogula commented 9 years ago

@tojocky sure, done !

dougludlow commented 7 years ago

@tojocky I noticed that this is still open, it's recommended in node-printer to use pdfium. Is this still a viable solution for printing pdf's on windows?

tojocky commented 7 years ago

Hi @dougludlow I think it is a solution to use pdfium to print PDF on windows. Unfortunately I didn't have time to finish pdfium node port to be PROD ready. Feel free to test it and propose PR.

askie commented 7 years ago

+1

xioxin commented 7 years ago

Use "pdftops.exe" to convert "pdf" to "PostScript" Use RAW Print

tool download: " Xpdf tools " http://www.xpdfreader.com/download.html