1

How to generate asci file to virtual pdf printer in vb.net. I tried with the virtual printer "microsoft print to pdf" it didn't work. I also found the link below but I don't understand whether it can used with the language of the vb.net program. Please the best solution.

http://www.columbia.edu/~em36/ghostpcl.html

below is a link to share the asci file so it's easy to try Here's a link! I use a dosprinter evaluation copy and it can produce the same output according to the output of the dot matrix printer so I think there must be other ways or solutions to achieve the same result. below share the pdf file Here's a link!

Public Shared Function SendFileToPrinter(ByVal szPrinterName As String, ByVal szFileName As String) As Boolean
    ' Open the file.
    'FileStream and BinaryReader are disposable objects. Hence you need to dispose of these objects Better if you declare them with a Using statement, so there's a very good chance they're disposed even when an exception is generated in the meanwhile. -- Disposing of disposable objects is not exactly optional.
    Using fs As New FileStream(szFileName, FileMode.Open)
        ' Create a BinaryReader on the file.
        Dim br As New BinaryReader(fs)

        ' Dim an array of bytes big enough to hold the file's contents.
        Dim bytes(fs.Length - 1) As Byte
        Dim bSuccess As Boolean = False
        ' Your unmanaged pointer.
        Dim pUnmanagedBytes As New IntPtr(0)
        Dim nLength As Integer

        nLength = Convert.ToInt32(fs.Length)
        ' Read the contents of the file into the array.
        bytes = br.ReadBytes(nLength)
        ' Allocate some unmanaged memory for those bytes.
        pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength)
        ' Copy the managed byte array into the unmanaged array.
        Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength)
        ' Send the unmanaged bytes to the printer.
        bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength)
        ' Free the unmanaged memory that you allocated earlier.
        Marshal.FreeCoTaskMem(pUnmanagedBytes)
        Return bSuccess
    End Using
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim printer As String = "Microsoft Print to PDF"
    For i As Integer = 1 To 1
        SendFileToPrinter(printer, "C:\vDos\#LPT1.asc")
    Next i
    Me.Close()
End Sub

result1

result2

view asci file

result pdf from KJ

result pdf

roy
  • 693
  • 2
  • 11
  • I don't see any reason why this is tagged with Ghostscript and PostScript so have removed the tags. I don't see what the relevance of the link to unofficial GhostPCL documentation is either. Finally it is not clear what is expected as output; a PDF file ? A text file ? Something else ? "I tried with the virtual printer "microsoft print to pdf" it didn't work." In what way didn't work ? – KenS Jul 26 '22 at 14:53
  • @KenS `I tried with the virtual printer "microsoft print to pdf" it didn't work." In what way didn't work ? ` by sending the file directly to the virtual printer and I want to output the pdf file – roy Jul 26 '22 at 15:04
  • Not that I have an answer, since I know nothing about WinForms, but what happened when you tried it ? An error ? Something else ? You need to be clear about what problem you are experiencing. I suspect that, if anything SendFileToPrinter just sends the file straight to the printer. Unless your printer can handle simple text as an input (and the MS print to PDF cannot printer), then that API call won't work. You need to open an application which can handle the input file and knows how to print it, and ask it to do so. – KenS Jul 26 '22 at 15:36
  • Possibly this is a duplicate of https://stackoverflow.com/questions/6103705/how-can-i-send-a-file-document-to-the-printer-and-have-it-print or at least worth looking at. – KenS Jul 26 '22 at 15:36
  • @KenS , thanks reply from you, if I use "Microsoft Print to PDF" then I can output to pdf but I can't I open the file you can see result1 &result2. The question is not duplicated because the source of my file is an asci file that comes from the emulator dos program. – roy Jul 27 '22 at 06:25
  • @KJ , if I convert it to pdf then I don't need to print to dotmatriks and the pdf file can be a softcopy document – roy Jul 27 '22 at 12:37

1 Answers1

0

Your using Widows so unless your in a sandbox you can use native printing

write /pt file.doc "printer name" "printer driver name" "port address"

in this case your doc is an asc so could also be sent via notepad to the print driver.

and since your using "Microsoft Print to PDF" use that twice as both "printer name" "printer driver name", finally a file is a port of call.

write /pt "C:\vDos\#LPT1.asc" "Microsoft Print to PDF" "Microsoft Print to PDF" "c:\target\file.pdf"

enter image description here

Using NotePad has some differences but you require setting printer defaults so I have a printer called A4Lpdf which uses the driver and a fixed output and Write can be in colour whereas Notepad is any font as long as its Black. notepad /pt "..\desktop\document.asc" "A4LPDF"

enter image description here

Later Edit

On supplying a minimal sample of your ascii.PRN file it is clearer that your input is like Epson Control codes and simple conversion to PDF is usually a disaster.

So here is a snip from an adobe print to pdf, where the two most obvious fails are underline and italic but everything is wrong as its not an image of the ink head output.

enter image description here

This is condensed mode
T h i s i s d o u b l e - w i d t h
T h i s is elite m o d e
T h i s i s e m p h a s i z e d m o d e
T h i s i s i t a l i c t y p e
T h i s i s d o u b l e - s t r i k e m o d e
This is under line mode
T h i s s u p e r s c r i p t
T h i s i s s u b s c r i p t 

Thus we see the codes are not applied to the PDF format the same as to a matrix printer.

Potential Solutions

Either find an Epson PRN to PDF application, there are a few, (but never found a perfect one, especially for graphics art output). The codes are here https://files.support.epson.com/pdf/general/escp2ref.pdf,

OR using the printer manual plus above look-ups, use RTF replacements including images, for a close visual representation, and then use Write to print the output.rtf file to PDF.

ESC @ Initialize printer
ESC P Select 10.5-point, 10-cpi
ESC C Set page length in lines ($=how many = 36 ?)
ESC M Select 10.5-point, 12-cpi (not unusual to over-ride previous setting)
W1w1 Double width spacing (WIDE FONT ON) and Double height ON (thus 21 point characters)
ESC w0 W0 Turn double-height and width printing off

However basic PDF Emulation will never be like the hardware page inked image enter image description here

{\rtf1\ansi\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Perfect DOS VGA 437 Win;}{\f1\fnil\fcharset0 System;}}
{\*\generator Riched20 10.0.22000}\viewkind4\uc1 
\pard\b\f0\fs14\lang2057                                                                        24 May 22\par
\par
                                                                    KANA                     \par
                                                                                             \par
                                                                                             \par
                                                                                             \par
  PG: 1                                        \fs28 SI12202041\f1\fs21                         \par
\f0\fs14\par
\par
\par
\par
     1      25 Pcs   USB CABLE                                    260.000,00     6.500.000,00\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
\par
                                                                                  6.500.000,00\page\f1\fs21\par
}

Its all down to finding the correct font metrics, since line printers were non-proportional fonts, but the line spaces are the fly in the ointment, they need to be pitch perfect. Thus you will need to write or find a font that EXACTLY matches the source printer styles of 10.5 points by 12cpi and 21 pts x 6 cpi (nominal) then tune them. Above is using a rough DOS VGA screen font but then I needed to adjust scales as I apply it as 7 and 14 to emulate the printers 10.5 and 21 by 70%.

It is a sloppy first shot that could be cleaned up, the key points are that RTF units are doubled expected eg 14 units = 7 pts and the language is a mix between others so \par = paragraph or line feed and the font I used is not the best but was quick to find and double click to install.

You need to read the byte codes then ignore or convert into rtf so ignoring the start printer code, replace with the string I give above using your choice of installed font name for replacing the font control codes, then convert line feeds to \par etc.

This could all be scripted in a console batch file or any language, if you store a common header (or preset pre-pends) then just change the body line by line with \par and finally add } as the trailer.

K J
  • 8,045
  • 3
  • 14
  • 36
  • thank you reply from you, I attach a screenhot "view asci file" even though in it there is a character code that if I use directly print to the printer dot matrix then the character code does not appear and the result of the answer from you I attach in the screenshot "result pdf from KJ", It should be the same pdf result as the output result that I use directly to the printer – roy Jul 27 '22 at 06:31
  • You see the screenshot or pdf file that I shared and that I attached it using the evaluation copy dosprinter – roy Jul 27 '22 at 12:32
  • I won't use it because it uses software that is paid and I want to make it that way. That's why I need a master solution from you and others – roy Jul 27 '22 at 12:40
  • I see the results from you are also good a little more perfect stage – roy Jul 27 '22 at 12:55
  • the way you do what can i do in the language of the program vb.net – roy Jul 27 '22 at 13:07
  • I see the screenshot you did it was so perfect – roy Jul 27 '22 at 13:40
  • please guide me so I can make a result as you did – roy Jul 27 '22 at 13:44
  • can you create a bat file in your way so that I am easy if I want to replace it with a different program language – roy Jul 27 '22 at 14:12
  • Thank you for your recommendation, I will try it later. – roy Jul 27 '22 at 15:05