Pradeep
Pradeep

Reputation: 11

How to save as PDF while printing a document on actual printer

I am trying to find a generalized solution on how to save the document as PDF while giving print on my actual printer on Windows 7/10 OS. Scenario: Whatever I am printing whether an image or a file or from the third party app like POS or Screen Print (like user press pint button on the third party application or press CTRL+P if the third party app supported this hot key), i need to save the document as PDF which this document is reaching to the actual printer which is printing the hard copies. I wanted to generalized to support all kinds of printers be it dot matrix or laser or thermal etc...

Solution which I have tried out:

  1. Virtual Printer which will print pdf and then route the pdf to the actual printer. But few printers doesn't support PDF like Thermal printer do not support PDF, moreover for this i need to take care of the paper settings and page settings. I have tried Virtual Printers like Win2PDF / ClawPDF
  2. I have looked into the Windows Printing architecture, and tried saving spool file and listening the printing event and routing the spool file to virtual printer to create pdf. This works partially as some of the printer drivers emits the RAW Spool file. PCL & Postscript can be fine to convert to PDF.
  3. Using Print processor like PrintMulti but the same issue like the 2nd one.
  4. I have tried to look into the even if I can get some kind of input file from which Windows Printing creating the Spool file, but I am unable to get that.
  5. I tried Printer Pooling but that is like a load sharing kind of things.

Could anyone help me on this. I am unable to find any solution over google as well so posting it here. It looks a normal thing to achieve but even after two weeks i am unable to find any solution for this. Is this something difficult to achieve the generalized solution?

Upvotes: 1

Views: 1451

Answers (1)

Hussam Barouqa
Hussam Barouqa

Reputation: 61

Consider an approach using something like the ePrint Virtual Printer which works by capturing the Windows EMF file from the print job and converting it to PDF while also being able to still send the EMF to the physical printer and print it. The application itself allows for the creation of Print Tasks in a Virtual Printer, which can be configured to save to PDF and batch print to other printers.

On the other hand, ePrint is based on the LEADTOOLS Virtual Printer SDK, which could be useful for you to look into if you are looking to code your own approach.

For example, the code for an event that is hooked to a virtual printer to save the captured EMF as PDF:

// Write the EMF as file to disk as PDF.
static void VirtualPrinter_EmfEvent( object sender, EmfEventArgs e )
{
   string pdfPath = Path.Combine(
      @"c:\Output\PDF\",
      Path.GetFileNameWithoutExtension( Path.GetRandomFileName() )
      ) + ".pdf";
   Directory.CreateDirectory( Path.GetDirectoryName( pdfPath ) );
            
   // Create an instance of the LEADTOOLS DocumentWriter
   DocumentWriter docWriter = new DocumentWriter();
   docWriter.BeginDocument( pdfPath, DocumentFormat.Pdf );
   DocumentEmfPage page = new DocumentEmfPage() {
      EmfHandle = new Metafile( e.Stream ) )
         .GetHenhmetafile()
   };
   docWriter.AddPage( page );
   docWriter.EndDocument();
}

If you are interested in more details, you can check out this article here.

Upvotes: 0

Related Questions