printing to the default printer???

Just starting out? Need help? Post your questions and find answers here.
stmdbe2019
User
User
Posts: 89
Joined: Mon Aug 31, 2009 2:11 pm

printing to the default printer???

Post by stmdbe2019 »

I have a printer Toshiba which is for ticket printing. I tried GhostScript but that fails with this printer and i do not want to use my Java codes anymore for this project.
So i was trying PureBasic 5.62 Beta 1 for printing simple hello world to the printer. But i am getting sick with the default printer selection dialog window is coming on every print request.

1. Run

Image

2. Print selection popup showing:
Image

3. Printing:
Image

Question.
=
How to print to the printer, without giving the choice in point 2 screenshot?
-----
Registered PureBasic Coder.
stmdbe2019
User
User
Posts: 89
Joined: Mon Aug 31, 2009 2:11 pm

Re: URGENT - printing to the default printer???

Post by stmdbe2019 »

Can you tell instead of `DefaultPrinter()` like Printer( NAME ??????????) ???

I have three printer connected 1 printer for parking ticket, 2nd printer for movie ticket 3rd printer for receit
-----
Registered PureBasic Coder.
stmdbe2019
User
User
Posts: 89
Joined: Mon Aug 31, 2009 2:11 pm

Re: URGENT - printing to the default printer???

Post by stmdbe2019 »

it Works.

Image

Code: Select all

Procedure AlignText(X,YAlign,Text.s,Font,RGB=-1,BackRGB=-1)
     ; ;Without  RGB Param or RGB=-1, keeps previous RGB. Default=#Black
     ; ;Without BackRGB Param or BackRGB=-1, keeps previous BackRGB. Default=#White
     Static X1,Y1,OldRGB,OldBACK
     FontID=FontID(Font)
     DrawingFont(FontID)
     If RGB=-1 : RGB=OldRGB: EndIf
     If BackRGB=-1
          If OldBACK=RGB:BackRGB=#White
          Else :  BackRGB=OldBACK
          EndIf
     EndIf
     If X=-1
          If X1=0:X=0
          Else:X=X1
          EndIf
     EndIf
     If YAlign=-1
          If Y1=0:YAlign=0
          Else:YAlign=Y1
          EndIf
     EndIf

     ; Select the font, Get the Font Metrics, and then reselect the current object
     hOld.l = SelectObject_(_DRAWING,FontID)
     GetTextMetrics_(_DRAWING, TM.NEWTEXTMETRIC)  ;to change text bottom position
     If hOld <> 0 : SelectObject_(_DRAWING,hOld):EndIf
     
     
     
     X1=DrawText(X,YAlign-TM\tmAscent,Text,RGB,BackRGB)
     Y1=YAlign
     
     OldRGB=RGB :  OldBACK=BackRGB
     ProcedureReturn X1 ; x position for the next string
   EndProcedure
      
   
   Dim Font(5)
   Font(0) = LoadFont(-1, "Arial", 26)
   
   
; Printer - default
If DefaultPrinter()         
  If StartPrinting("Test")
    If StartDrawing(PrinterOutput())
      DrawingMode(#PB_2DDrawing_Transparent)
      AlignText(10,10, "Hello World. PureBasic Printer printing", Font(0), #Black)
      StopDrawing()
    EndIf
   
  StopPrinting()
  EndIf
 
EndIf
-----
Registered PureBasic Coder.
User avatar
Fig
Enthusiast
Enthusiast
Posts: 352
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

Re: URGENT - printing to the default printer???

Post by Fig »

So, to summarize, Pb can ask for the printer you want to use or use the default one.
But Pb can't let you select the printer you want to use by its name. (if it's not the default one)

So this last feature is missing and should be add. Am i right ? :?:
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS
stmdbe2019
User
User
Posts: 89
Joined: Mon Aug 31, 2009 2:11 pm

Re: URGENT - printing to the default printer???

Post by stmdbe2019 »

YES - that was exactly what is missing or i could not find any reference. In Java you can do that but i hate Java too much to write for simple tasks, i want it in PureBasic.

Code: Select all

import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.MediaTray;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPageable;

public class PMticket {

  public static boolean saveFile(URL url, String file) throws IOException {
    boolean download_status = false;

    System.out.println("[OK] - open");
    InputStream in = url.openStream();
    FileOutputStream fos = new FileOutputStream(new File(file));
    System.out.println("[OK] - reading file...");
    int length = -1;
    byte[] buffer = new byte[1024];

    while ((length = in.read(buffer)) > -1) {
        fos.write(buffer, 0, length);
    }
    fos.close();
    in.close();

    download_status = true;
    System.out.println("[OK] - downloaded");
    return download_status;
  }

  public static void main(String[] args) throws IOException, PrinterException {  
    // Brother HL-6180DW series   
    // TOSHIBA B-FV4 (203 dpi)
    String downloaded_filename = "C:/Python27/pdf_ticket.pdf";
    String download_pdf_from = "http://xx.xxx.com/index/pmtprint?print=" + args[0];
    //String download_pdf_from = "http://xx.xxxx.com/index/pmtprint?print=777" ;
    String downloaded_filename_open_as_pdf = "C:\\Python27\\pdf_ticket.pdf";
    String printerNameDesired = "TOSHIBA B-FV4 (203 dpi)";
    //String printerNameDesired = "Brother HL-5350DN series";
    
    // Get printers
    PrintService[] services = PrinterJob.lookupPrintServices();
    DocPrintJob docPrintJob = null;
    for (int i = 0; i < services.length; i++) {
      System.out.println(services[i]);
    }   
    
    try{
      URL url = new URL(download_pdf_from);
    
      if(saveFile(url, downloaded_filename)) {
        try {
          PDDocument pdf = PDDocument.load(new File(downloaded_filename_open_as_pdf));
          PrinterJob job = PrinterJob.getPrinterJob();
          for (int i = 0; i < services.length; i++) {
           if (services[i].getName().equalsIgnoreCase(printerNameDesired)) {
             docPrintJob = services[i].createPrintJob();
           }
          }
             
          job.setPrintService(docPrintJob.getPrintService());
          job.setPageable(new PDFPageable(pdf));
          job.print();

        } catch (Exception e) {
          System.out.println("[FAIL]" + e);
        }      
      } else {
        System.out.println("[FAIL] - download fail");
      }      
    } catch (Exception ae) {
      System.out.println("[FAIL]" + ae);
    }


  }
}
-----
Registered PureBasic Coder.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: URGENT - printing to the default printer???

Post by RASHAD »

Try
Only for Windows
Tested PB 5.62 x86 - Windows 10 x64
Example : Print to "Microsoft Print to PDF" direct

Code: Select all


Procedure Setdefaultprinter_(DeviceLine.s) 
  WriteProfileString_("windows", "Device", DeviceLine) 
  SendMessage_(#HWND_BROADCAST, #WM_WININICHANGE, 0, "windows") 
EndProcedure

Procedure SetPrinter(printername$)
  enumFlags = #PRINTER_ENUM_LOCAL | #PRINTER_ENUM_CONNECTIONS
  If EnumPrinters_(enumFlags, #Null, 1, 0, 0, @deviceNamesBufferSize, @printerCount) = 0
    deviceNamesBuffer = AllocateMemory(deviceNamesBufferSize)
    If deviceNamesBuffer And EnumPrinters_(enumFlags, #Null, 1, 
       deviceNamesBuffer, deviceNamesBufferSize, @sizeRDB, @printerCount)
      If printerCount
        pInfoNameOffset = OffsetOf(PRINTER_INFO_1\pName)
        For i = 0 To (printerCount - 1)
          If PeekS(PeekL(deviceNamesBuffer + i * (SizeOf(PRINTER_INFO_1)) + pInfoNameOffset)) = printername$
            Setdefaultprinter_(printername$)
            Break
          EndIf
        Next
      EndIf
      FreeMemory(deviceNamesBuffer)
    EndIf
  EndIf  
EndProcedure

LoadFont(0,"Tahoma",12)

OpenWindow(0,0,0,200,100,"Printer Test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  ButtonGadget(1,10,10,60,20,"Print")

   
Repeat
  Select WaitWindowEvent()
      
      Case #PB_Event_CloseWindow
            Quit = 1
      
      Case #PB_Event_Gadget
          Select EventGadget()
           Case 1
              SetPrinter("Microsoft Print to PDF")
                If DefaultPrinter() 
                  If StartPrinting("PureBasic Test")
                   
                    If StartVectorDrawing(PrinterVectorOutput())                   
                      VectorFont(FontID(0), 16)
                      VectorSourceColor($FF7982FF)
                      
                      MovePathCursor(0, 30)
                      DrawVectorText("Hello RASHAD")
                     
                      StopVectorDrawing()
                    EndIf                   
                    StopPrinting()
                  EndIf
                EndIf  
          EndSelect          

  EndSelect 

Until Quit = 1
End 
Egypt my love
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: printing to the default printer???

Post by Fangbeast »

Mr RASHAD, I still have no reply to your email? The one you gave me in PM ages ago.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: printing to the default printer???

Post by RASHAD »

Hi Fang :)
Sorry for late
I am so thick these days(diabetic)
But give me a chance for this week end (I hope i will fulfill my promise)
Egypt my love
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: printing to the default printer???

Post by Fangbeast »

I am so thick these days(diabetic)
I am a type II diabetic but manage to keep it mostly under control with exercise and Metaformin.

I do sometimes get back and sneak in a muffin or some maltesers.

Sorry for offtopic folks.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
MarcoAb
New User
New User
Posts: 5
Joined: Wed Aug 10, 2022 6:40 am

Re: URGENT - printing to the default printer???

Post by MarcoAb »

Thanks for me it was really useful. Is it possible to pass the name of the pdf file to be saved?
Thanks in advance.
Post Reply