Page 1 of 1

printing to the default printer???

Posted: Sat Mar 24, 2018 9:14 pm
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?

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

Posted: Sat Mar 24, 2018 9:16 pm
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

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

Posted: Sat Mar 24, 2018 9:35 pm
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

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

Posted: Sun Mar 25, 2018 9:18 am
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 ? :?:

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

Posted: Mon Mar 26, 2018 6:28 am
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);
    }


  }
}

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

Posted: Mon Mar 26, 2018 6:54 am
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 

Re: printing to the default printer???

Posted: Mon Mar 26, 2018 8:06 am
by Fangbeast
Mr RASHAD, I still have no reply to your email? The one you gave me in PM ages ago.

Re: printing to the default printer???

Posted: Mon Mar 26, 2018 8:35 am
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)

Re: printing to the default printer???

Posted: Mon Mar 26, 2018 9:10 am
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.

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

Posted: Tue Sep 13, 2022 7:06 am
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.