cwindowsprintingprint-spooler-api

Programatically print PDF file using Windows Spooler API


Is it possible to print a PDF file from using windows Spooler API. I tried with below code and it is not working...

int print_handle = 0;   
OpenPrinter(pPrinterName, &print_handle, NULL);
if (print_handle == 0)
{
    return 0;
}
docinfo1.pDocName = (LPTSTR)("My PDF");
docinfo1.pOutputFile = NULL;
docinfo1.pDatatype = (LPTSTR)("RAW");
temp = StartDocPrinter(print_handle, 1, &docinfo1);
temp = StartPagePrinter(print_handle);
temp = WritePrinter(print_handle, (LPBYTE)filebuff, filelen, &bytes_written);
EndPagePrinter(print_handle);
EndDocPrinter(print_handle);

WritePrinter function return SUCCESS and nothing got printed. Printing TXT and PRN file using this API is working.


Solution

  • Windows has no built-in ability to print PDF files.

    You'll have to use an external application or library, e.g. Ghostscript.

    I should note that because your code uses the "RAW" datatype (like the official sample) it is sending the written job data directly to the printer. ASCII data works because practically all printers can receive and print ASCII. If the printer understood PDF as a PDL (Page Description Language - other examples are PCL or PostScript), it would print your PDF. But for all other printers the PDF needs to be converted into a PDL the printer understands.