IBM HP5Si Print Stream to XPS print driver

115 Views Asked by At

I am hoping someone can have suggestions about this issue. We have a custom driver taken from https://learn.microsoft.com/en-us/samples/microsoft/windows-driver-samples/xpsdrv-driver-and-filter-sample/

The print driver works well and outputs XPS when the documents are opened in MS word or PDF. But when a document is printed from HP5Si series printer, the driver returns 0 bytes. The job is sent from HP5Si printer to the XPS driver. Why is the driver rejecting this input when the source is a HP series printer. What can I do to fix it?

The printer on the AS400 is setup with an IBM HP5Si driver and sends the job to a windows service on a server. This windows service routes the job to XPS driver as if it were an HP series printer. The XPS driver processes this job and returns XPS to the windows service. The windows service then converts to a tiff file.

For some reason if printing is done using this workflow XPS driver returns 0. If the same document is opened in word or notepad or any not AS400+ HP, it works and XPS is returned. To prove my theory, I sent a PCL file in C# code to the driver and it returned 0 bytes.

    public static void SendBytesToPrinterPCL(string printerName, string szFileName) {
    IntPtr lhPrinter;
    OpenPrinter(printerName, out lhPrinter, new IntPtr(0));

    if (lhPrinter.ToInt32() == 0) return; //Printer not found!!

    var rawPrinter = new DOCINFOA() {
        pDocName = "My Document",
        pDataType = "RAW"
    };

    StartDocPrinter(lhPrinter, 1, rawPrinter);

    using(var b = new BinaryReader(File.Open(szFileName, FileMode.Open))) {
        var length = (int) b.BaseStream.Length;
        const int bufferSize = 8192;

        var numLoops = length / bufferSize;
        var leftOver = length % bufferSize;

        for (int i = 0; i < numLoops; i++) {
            var buffer = new byte[bufferSize];
            int dwWritten;

            b.Read(buffer, 0, bufferSize);
            IntPtr unmanagedPointer = Marshal.AllocHGlobal(buffer.Length);
            Marshal.Copy(buffer, 0, unmanagedPointer, buffer.Length);
            WritePrinter(lhPrinter, unmanagedPointer, bufferSize, out dwWritten);
            Marshal.FreeHGlobal(unmanagedPointer);
        }

        if (leftOver > 0) {
            var buffer = new byte[leftOver];
            int dwWritten;

            b.Read(buffer, 0, leftOver);
            IntPtr unmanagedPointer = Marshal.AllocHGlobal(buffer.Length);
            Marshal.Copy(buffer, 0, unmanagedPointer, buffer.Length);
            WritePrinter(lhPrinter, unmanagedPointer, leftOver, out dwWritten);
            Marshal.FreeHGlobal(unmanagedPointer);
        }
    }

    EndDocPrinter(lhPrinter);
    ClosePrinter(lhPrinter);
}
string filePath = @"C:\Users\tom\Desktop\form.PCL";
string szPrinterName = @"\\server\xpsdrv";

Print.SendBytesToPrinterPCL(szPrinterName, filePath);

Then I sent a regular text file to the driver and it successfully converted to XPS.

 public static void SendToPrinterNonPCL(string filePath)
            {
                ProcessStartInfo info = new ProcessStartInfo();
                info.Verb = "print";
                info.FileName = filePath;
                info.CreateNoWindow = true;
                info.WindowStyle = ProcessWindowStyle.Hidden;

                Process p = new Process();
                p.StartInfo = info;
                p.Start();

                p.WaitForInputIdle();
                System.Threading.Thread.Sleep(3000);
                if (false == p.CloseMainWindow())
                    p.Kill();
            }
 string filePath = @"C:\Users\tom\Desktop\form.txt";
    string szPrinterName = @"\\server\xpsdrv";
          
            Print.SendToPrinterNonPCL(filePath);

Why doesn't the driver in Microsoft samples accept PCL? What should I do. I am not a driver developer. This project was given to me.

EDIT: Initally I didn't know of this printing from AS400. Our legacy driver was built 15 years back. The developer wrote a custom print driver to PCL and a Custom converter to Tiff. But the driver only supported monochrome. I am not a driver expert or a PCL expert or a converter expert. In order to support color and less pixelated feel for the final Tiff, I decided to change it to a XPS driver. Also it is less custom code and could use Microsoft's XPS conversion in WPF. It is not a very big learning curve for a non-driver development person compared to learning PCL and then changing the converter to accomodate color Tiff. But I guess it is falling apart since the users also print from AS400 which sends PCL.

Do you know any good products which we could purchase a license to? We need a PCL driver and a converter to Tiff

Thank you

0

There are 0 best solutions below