Download PDF via Putty Terminal

152 views
Skip to first unread message

Mario H. Sabado

unread,
Mar 16, 2026, 5:22:06 AM (2 days ago) Mar 16
to 'elch' via Harbour Users
Hi,

I'm trying to download PDF file via Putty Terminal on my linux application (using lpansi and Przemek's  FWrite function).  I configured the ANSI printer in the putty session as "XPS Document Printer".  Text file output has no problem.  For PDF file with .xps extension, I noticed a 2kb size difference from the original file.  When I rename the file extension to .pdf and open, it displays a blank page.

Maybe anyone has a solution for this?

I attached the original and downloaded version of the file.

Thank you and would appreciate any help.

Best regards,
Mario

test.pdf
test_downloaded_renamed.pdf

Francesco Perillo

unread,
Mar 16, 2026, 5:29:33 AM (2 days ago) Mar 16
to harbou...@googlegroups.com
Hi Mario, I don't have the answer but I'm interested in "printing via putty".
Can you please point me to the code you refer in the message, please?
Thank you
Francesco 

--
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
Unsubscribe: harbour-user...@googlegroups.com
Web: https://groups.google.com/group/harbour-users
---
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/harbour-users/CAGuBOFpLk_BmsTNoxYpwVxk3x9QRgWo-81NeF%3DPJMvekYjP3mA%40mail.gmail.com.

Mario H. Sabado

unread,
Mar 16, 2026, 5:35:11 AM (2 days ago) Mar 16
to harbou...@googlegroups.com
Hi Francesco, 

Below is the function for redirecting output to local terminal client.  This function was provided by Przemek long before.

***************************************
FUNCTION PrintToTerminal( cPrinterData )
***************************************
      FWrite( 1, Chr( 27 ) + "[5i" + ;
                 cPrinterData + ;
                 Chr( 27 ) + "[4i" )
   RETURN NIL
****************************************

image.png

Best regards,
Mario


Francesco Perillo

unread,
Mar 16, 2026, 5:36:53 AM (2 days ago) Mar 16
to harbou...@googlegroups.com
Just to add that in the past I tried to port my console app to linux so that I could remove samba and hide the dbf files.

Printing was my problem. At the time I decided that I could create the PDF on the server, in a specific directory, and expose the file via samba (this time on a read-only share) or via apache. The application would print the UNC/URL on screen and the user could copy/paste it.

Mario H. Sabado

unread,
Mar 16, 2026, 5:44:55 AM (2 days ago) Mar 16
to harbou...@googlegroups.com
Hi Francesco,

I've been doing it before but it only works on dot matrix printer.  When it does not work with USB printers, you can install "Generic Text Printer" and configure the printer to output to file.  Here's my linux application sample that prompts the report to print via local terminal.

image.png

Best regards,
Mario

Francesco Perillo

unread,
Mar 16, 2026, 6:01:03 AM (2 days ago) Mar 16
to harbou...@googlegroups.com
Did you try to use the "Generic Text Printer" to "copy" the pdf locally ?
I fear that the XPS driver creates.... xps files...

Here is a nice solution, something I also thought in the past but deemed overkill... it is interesting since it can be used for other needs.
The idea is to:
- create a printer on every windows machine, with a "GENERIC TEXT PRINTER" (read as: every byte received is used as is...) with a printer port set to localhost and a port you decide to use.
- create a daemon that listens to that port

When the server starts to send bytes to putty, they are funnelled into the text printer that delivers to the daemon.
So you can create a "language" to let the server talk to the client...

Anyway, I'd test to use Generic text printer instead of xps...



Mario H. Sabado

unread,
Mar 16, 2026, 6:13:14 AM (2 days ago) Mar 16
to harbou...@googlegroups.com

For dot matrix printer or generic text printer, this should work out of the box using the FWrite.  

I tried the "PDF Printer" but I received 0 byte file.  Only with "XPS Doc Writer"  that I was able to received PDF (or other binary like XLSX) with almost identical file content, except for the 2kb extra that maybe causing not to be recognized by PDF Viewer.  Maybe you can help identify the difference from my sample file and strip the excess bytes and recognize it as a valid PDF format.  Once this is resolved, maybe the same should apply to other binary files like XLSX or images.  

Best regards,
Mario 

Francesco Perillo

unread,
Mar 16, 2026, 6:27:00 AM (2 days ago) Mar 16
to harbou...@googlegroups.com
The files differ by the EOL char used...
I opened them in VIM on windows, on the left TEST:PDF, on the right the wrong one. There are 1811 lines so about 2k lines, so 2k more chars, shown as ^M on the left....
I really don't know if this is the problem but I tried to tell vim to save the file as unix line ending and it now is 157kb, as test.pdf.
Unfortunately it doesn't open yet, so probably there is some other stuff breaking, but it may be vim...

immagine.png


Mario H. Sabado

unread,
Mar 16, 2026, 6:33:40 AM (2 days ago) Mar 16
to harbou...@googlegroups.com
Thanks for this hint Francesco.  Maybe a good start.  I'm trying to receive the file as a pdf so that the user can not alter the report.

For text output report and received on local terminal as .xps file, you can default the application to Edge browser to render even with wide format.


image.png

Mario H. Sabado

unread,
Mar 16, 2026, 6:36:31 AM (2 days ago) Mar 16
to harbou...@googlegroups.com

Francesco, here's the unaltered PDF file received as xps document.  Maybe you can have better comparison on spotting the difference.
PDF_unaltered.oxps

Francesco Perillo

unread,
Mar 16, 2026, 6:47:59 AM (2 days ago) Mar 16
to harbou...@googlegroups.com
I downloaded the oxps file, changed the suffix in .pdf, the page doesn't show

I open it in vim, the type ":set ff" and it reports FF=DOS
then I type ":set ff=unix" followed by ":w" In this way I save it in unix EOL.

Now the file opens correctly

So the problem is with the line endings

Francesco

Mario H. Sabado

unread,
Mar 16, 2026, 8:16:49 AM (2 days ago) Mar 16
to harbou...@googlegroups.com
Hi Francesco,

That's nice to hear.  So maybe changing CRLF EOL to LF has solved the problem?

Mario H. Sabado

unread,
Mar 16, 2026, 9:09:14 AM (2 days ago) Mar 16
to harbou...@googlegroups.com
Hi Francesco,

I tried to replace CRLF with LF from xps and save it as pdf but still unable to open the file as pdf.  

Mario H. Sabado

unread,
Mar 16, 2026, 10:25:21 AM (2 days ago) Mar 16
to harbou...@googlegroups.com
Hi Francesco,

Finally figured it out.
******************************************************************
Hb_MemoWrit("pdf_altered.pdf", ; //write to output pdf
                       Hb_StrReplace( Hb_MemoRead("pdf_unaltered.oxps"), {chr(13)+chr(10)}, {chr(10) }) ) //replace CRLF with LF  
*******************************************************************

Best regards,
Mario

Mario H. Sabado

unread,
Mar 16, 2026, 11:39:42 AM (2 days ago) Mar 16
to harbou...@googlegroups.com
Hi Francesco,

As per further testing, "Generic Text Printer" as a prn file also works like xps when printing a pdf file from a remote terminal.  With "PDF Printer", the downloaded file is 0 byte size.

Please note that when the connected Text Printer is an actual dot matrix printer, the downloaded data should print directly to that printer.  You may test with other printer types if it already works since my experience before only works with dot matrix.

Thanks,
Mario

Mario H. Sabado

unread,
Mar 16, 2026, 11:25:53 PM (2 days ago) Mar 16
to harbou...@googlegroups.com
Hi Francesco,

Here's the solution that I have now.

*******************************************
FUNCTION PdfXps2Pdf(cPdfXpsFile)  //Convert PDF saved as XPS/PRN to PDF file
*******************************************
local cPdf,cOutFile,s

if cPdfXpsFile!=Nil .and. Hb_vfExists(cPdfXpsFile)
   s:=Hb_MemoRead(cPdfXpsFile)

   if ("PDF" $ s) //check if valid PDF
      //Hb_Alert({cPdfXpsFile+" is a valid PDF file!"})    
      cPdf:=HB_StrReplace((cPdfXpsFile),{".oxps",".prn"},{".pdf",".pdf"})  //pdf output file
      Hb_FNameSplit(cPdf, ,@cOutFile)
      cOutFile:="file:///"+ Hb_StrReplace(GetEnv("USERPROFILE"),"\","/")+"/Documents/"+cOutFile+".pdf"
      Hb_MemoWrit( cPdf, Hb_StrReplace( s,{chr(13)+chr(10)},{chr(10)} ) ) //replace CRLF with LF then write to cPdf
  else
     cOutFile:=cPdfXpsFile //open original file
  endif
  s:="" //reset
  wapi_SHELLEXECUTE( 0,"Open" ,"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"  ,'"'+cOutFile+'"', 0 , 1 )
endif
return nil
*******************************************************************************************************
image.png


Would appreciate if you suggest a cleaner and more generic approach?

Thanks,
Mario

poopall

unread,
12:10 AM (13 hours ago) 12:10 AM
to Harbour Users
You could write the code in PCL5 and then copy the file to a pcl5/6 etc compatible printer. I dont know about putty, but this may work. I use a ghostscript to convert pcl5 to pdf if required

Mario H. Sabado

unread,
1:54 AM (11 hours ago) 1:54 AM
to harbou...@googlegroups.com
Hi,  thanks for the suggestions. 

I tried FreePDF-XP and the received data just queued on the print manager and disappeared right away.   

For the FreePDF 2025, the received data opens automatically on its viewer and displays the contents but I cannot adjust the page size to accommodate the actual view of the report. 

So far, the "XPS Document Writer" was able to receive data and dump it to the user documents folder.  If you're fast enough, you can double click the received data from the notification and automatically open and render the file using MS Edge browser. 

For the PCL format, I have not tried it yet because the intention is to receive the printed data in PDF format to avoid alteration from the user's end.

The application runs on a linux remote terminal server via SSH using putty.  The putty client currently supports local printing redirection using ANSI printer so It supports direct printing to dot matrix printers.

Best regards,
Mario
   

Reply all
Reply to author
Forward
0 new messages