edabg / jsprintsetup

JSPrintSetup Firefox addon
Mozilla Public License 2.0
77 stars 39 forks source link

toFileName not working on Windows FF 47.0.1 #20

Closed peekpeekpeekpeek closed 8 years ago

peekpeekpeekpeek commented 8 years ago

I tried several different Printer drivers, and even added the printToFile bool value to jsPrintSetup.js (did not help, if I make it true, I get error). I tried every kOutputFormat, and different filepaths. It always prompts for filename, and doesn't even set the default filename or path to save to.

    var jsprint_filename = 'C:\Temp\test.pdf';

    jsPrintSetup.setPrinter('Adobe PDF');

    jsPrintSetup.setOption('outputFormat', jsPrintSetup.kOutputFormatPS);

// jsPrintSetup.setOption('printToFile', 1); jsPrintSetup.setOption('toFileName', jsprint_filename);

    jsPrintSetup.setOption('orientation', jsPrintSetup.kPortraitOrientation);

    jsPrintSetup.setOption('headerStrLeft', '');
    jsPrintSetup.setOption('headerStrCenter', '');
    jsPrintSetup.setOption('headerStrRight', '');

    jsPrintSetup.setOption('footerStrLeft', '');
    jsPrintSetup.setOption('footerStrCenter', '');
    jsPrintSetup.setOption('footerStrRight', '');

    jsPrintSetup.clearSilentPrint();
    jsPrintSetup.setOption('printSilent', 1);

    jsPrintSetup.print();

    jsPrintSetup.setOption('printSilent', 0);

The filename can be set by a C++ test print program which uses lpszOutput member of DOCINFO calling StartDoc. So this is not a Windows bug.

------- UPDATE I just tested using older builds of FF. Using builds 24.6, 38.7 and 45.2. They all have the same issue, print filename is never set.

mitkola commented 8 years ago

On windows kOutputFormatPDF output should work. I've noted that you have slashes in path to output file which are escape character also, you must double them. Here is working example (Win 10/64, FF 47.0.1):

jsPrintSetup.setOption('outputFormat', jsPrintSetup.kOutputFormatPDF);
jsPrintSetup.setOption('toFileName', 'D:\\Temp\\_test.pdf');
jsPrintSetup.clearSilentPrint();
jsPrintSetup.setOption('printSilent', 1);
jsPrintSetup.print();
jsPrintSetup.setOption('printSilent', 0);
peekpeekpeekpeek commented 8 years ago

Once I fixed the escape characters, the PDF test worked fine. But sadly my goal was actually to print Postscript using a generic postscript PPD.

When I use that as my printer, then both kOutputFormatPS and kOutputFormatNativeI fail, I get the dialog. If I use kOutputFormatPDF, with a postscript driver, no prompt.... but I get a tiny invalid PDF file.

Attached is my generic PPD MYPS.zip

mitkola commented 8 years ago

I think that there is no native output to PS on windows. https://dxr.mozilla.org/mozilla-beta/source/widget/windows/nsDeviceContextSpecWin.cpp#230 While in Linux GTK the PS is default behavior. https://dxr.mozilla.org/mozilla-beta/source/widget/gtk/nsDeviceContextSpecG.cpp#105

You can use some PS to file printer driver to print in PS file. But toFileName option can't set output file for this driver.

peekpeekpeekpeek commented 8 years ago

What does kOutputFormatNative do? I figured it was so you could use any printer driver, and sent the output to a file by specifying the filename with toFileName.

Windows does include a generic Postscript driver, but you have to install it as a another printer device using a generic PPD.

Looking through the source, I see docinfo.lpszOutput set from the aPrintToFileName passed to BeginPrinting https://dxr.mozilla.org/mozilla-beta/source/gfx/thebes/gfxWindowsSurface.cpp#208

BeginPrinting is called from here: https://dxr.mozilla.org/mozilla-beta/source/gfx/src/nsDeviceContext.cpp#477

SetupToPrintContent is a bit of a mess to follow where the filename passed to BeginPrinting actually comes from.

mitkola commented 8 years ago

After checking source code I found that there is attribute printToFile in nsIPrintSettings interface. Now have added this as option ti jsPrintSetup in new version https://github.com/edabg/jsprintsetup/releases/tag/v0.9.5.4 You can test with following code which works for me.

jsPrintSetup.setPrinter('Microsoft PS');
// I think that output format in this case has no sense, because is used printer driver to render it
//jsPrintSetup.setOption('outputFormat', jsPrintSetup.kOutputFormatPS); 
jsPrintSetup.setOption('printToFile', 1); // NEW!
jsPrintSetup.setOption('toFileName', 'D:\\Temp\\_test.psf');
jsPrintSetup.clearSilentPrint();
jsPrintSetup.setOption('printSilent', 1);
jsPrintSetup.print();
jsPrintSetup.setOption('printSilent', 0)

If all is OK I will publish release at addons.mozilla.org