Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   imprimir en impresora de red (https://www.clubdelphi.com/foros/showthread.php?t=77737)

ethangio 23-02-2012 09:14:34

imprimir en impresora de red
 
Buen dia,

Disculpen hace un tiempo abri un hilo ( aqui ) acerca de la impresion y las funciones son algo complejas, y pues no me quise quedar con esa espinita que no cortaba el papel y hacerlo por mi mismo,
ahora ya corta el papel cuando es la impresora esta local , pero en red no se como asignarle la impresora para mandar a imprimir, si mi impresora es de red o que tal si no esta en el com3 y esta en el com1 o com2 ....

Codigo:

Código Delphi [-]
var F : TextFile;
    i, j : integer;
    temp : string;
begin
     AssignFile(F, 'COM3'); // SI MI IMPRESORA ESTA EN RED ? O EN EL COM1, COM2 ?
     Rewrite(F);
     temp := 'hola 0';
     Writeln(F, temp);
     Writeln(F, #13+#10);
     Writeln(F, 'Hola linea 1');
     Writeln(F, #13+#10);
     Writeln(F, 'Hola despues del enter linea 2');
     Writeln(F, #13+#10);
     Writeln(F, #13+#10);
     Writeln(F, #13+#10);
     Writeln(F, #13+#10);//enter
     Writeln(F, #27+#109);//corte de papel
     CloseFile(F);
end;

ya probe con AssignPrm(F) pero con esto no corta la secuencia ni local ni en red aunque si imprime, si escribo directamente al puerto local (con AssignFile(F. 'COM3')) si corta ...

NOTA: Espero no haberlos enredado... saludos

newtron 23-02-2012 09:32:41

Hola.

Me da que esa solución solo es viable en impresoras locales porque no creo que puedas acceder al puerto COM de un terminal de la red de esta manera. Para poder acceder a las impresoras en red deberías usar el objeto Printer.

Saludos

ethangio 23-02-2012 17:16:03

Newtron pero entonces como mando la secuencia de corte :( ??

He probado estos ejemplos de codigo y no me sale :( :( :( .. Tengo en el Form 1 listview y 1 memo

Código Delphi [-]
procedure TForm1.Button1Click(Sender: TObject);
var F : TextFile;
    i, j : integer;
    temp : string;
begin
     //AssignFile(F, 'COM3'); //corta localmente e imprime
     AssignPrn(F); //solo imprime 
     Rewrite(F);

     // Ponemos título de las columnas
     temp := 'Columna1 Columna2 Columna3 Columna4 Columna5';
     Writeln(F, temp);
     Writeln(F,'Hola linea 1');
     Writeln(F,#13+#10);
     Writeln(F,'Hola despues del enter linea 2');
     Writeln(F,#13+#10);
     Writeln(F,#13+#10);
     Writeln(F,#13+#10);
     Writeln(F,#13+#10); // secuencia de enter
     Writeln(F,#27+#109); //secuencia de corte
     CloseFile(F);
end;

procedure TForm1.Button2Click(Sender: TObject);
var y, i, j : integer;
    temp : string;
begin
     Printer.BeginDoc;
     With Printer.canvas do
      Begin
           Printer.Title := 'El titulo del documento que quiero';
           Font.Name := 'MS Sans Serif';
           Font.Color := ClBlack;
           Pen.Color := ClBlack;
           Font.Size := 12;
           Font.Name := 'Times New Roman';
           Font.Size := 12;

           // imprimimos ListView
           temp := 'Titulo que sale escrito en el papel';
           y := 100;
           TextOut(10, y, temp);
           for i := 0 to ListView1.Items.Count - 1 do
            begin
                 inc(y, 20);
                 temp := ListView1.Items[i].Caption;
                 for j := 0 to ListView1.Items[i].SubItems.Count - 1 do
                     temp := temp + ' ' + ListView1.Items[i].SubItems[j];
                 TextOut(10, y, temp);
            end;
                 inc(y, 20);
                 TextOut(10, y, #27+#109);
      end;
     Printer.EndDoc;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  r: TRect;
  i: Integer;
begin
  with Printer do
    begin
      r := Rect(200,200,(Pagewidth - 200),(PageHeight - 200));
      BeginDoc;
      Canvas.Brush.Style := bsClear;
      for i := 0 to Memo1.Lines.Count do
      begin
       Canvas.TextOut(200,200 + (i *
                   Canvas.TextHeight(Memo1.Lines.Strings[i])),
                   Memo1.Lines.Strings[i]);
      end;

       Canvas.TextOut(200,200 + (9 *
                   Canvas.TextHeight(Memo1.Lines.Strings[9])),
                   #27+#109);

      Canvas.Brush.Color := clBlack;
      Canvas.FrameRect(r);
      EndDoc;
    end;
end;

procedure TForm1.Button4Click(Sender: TObject);
var
Handle: THandle;
N: DWORD;
DocInfo1: TDocInfo1;
s : string;
begin
if not OpenPrinter(PChar('\\ethan-PC\caja'), Handle, nil) then begin
ShowMessage('Printer «\\ethan-PC\caja» not found.');
Exit;
end;

with DocInfo1 do begin
pDocName := PChar('Document Name');
pOutputFile := nil;
pDataType := 'RAW';
end;

StartDocPrinter(Handle, 1, @DocInfo1);

s := 'First line of first page' + #13+#10;
WritePrinter(Handle, PChar(s), Length(s), N);
s := 'Second line of first page' + #13+#10;
WritePrinter(Handle, PChar(s), Length(s), N);
s := #27+#109;
WritePrinter(Handle, PChar(s), Length(s), N);

EndPagePrinter(Handle);

{s := 'First line of second page' + #13#10;
WritePrinter(Handle, PChar(s), Length(s), N);
s := 'Second line of second page' + #13#10;
WritePrinter(Handle, PChar(s), Length(s), N);
}
EndPagePrinter(Handle);

EndDocPrinter(Handle);

ClosePrinter(Handle);
end;

procedure TForm1.Button5Click(Sender: TObject);
var
  PrinterHandle: THandle;
  DocInfo: TDocInfo1;
  i: Integer;
  B: Byte;
  Escritos: DWORD;
  Str : String;
begin
  Str:='linea 1 ,,, '+#13+#10+'linea 2...'+#27+#109;
  if OpenPrinter(PChar('\\ethan-PC\caja'), PrinterHandle, nil) then
  try
    FillChar(DocInfo,Sizeof(DocInfo),#0);
    with DocInfo do
    begin
      pDocName:= PChar('Printer Test');
      pOutputFile:= nil;
      pDataType:= 'RAW';
    end;
    if StartDocPrinter(PrinterHandle, 1, @DocInfo) <> 0 then
    try
      if StartPagePrinter(PrinterHandle) then
      try
        while Length(Str) > 0 do
        begin
          if Copy(Str, 1, 1) = '\' then
          begin
            if Uppercase(Copy(Str, 2, 1)) = 'X' then
              Str[2]:= '$';
            if not TryStrToInt(Copy(Str, 2, 3),i) then
              Exit;
            B:= Byte(i);
            Delete(Str, 1, 3);
          end else B:= Byte(Str[1]);
          Delete(Str,1,1);
          WritePrinter(PrinterHandle, @B, 1, Escritos);
        end;
      finally
        EndPagePrinter(PrinterHandle);
      end;
    finally
      EndDocPrinter(PrinterHandle);
    end;
  finally
    ClosePrinter(PrinterHandle);
  end;
end;

jeje son mis ejemplos para imprimir :D .. Saludos

Newtron hay forma de saber por medio del nombre de la impresora local en que puerto esta conectada ???

newtron 24-02-2012 09:29:08

A ver....

Te pongo un ejemplo de una utilidad para abrir el cajón portamonedas, el corte de papel es exactamente igual solo poniendo los caracteres correspondientes. Lo he simplificado un poco para que te sea más legible.

Código Delphi [-]
// ****************************************************************************
// Rutina      : AbreElCajon
// Descripcion :
// ****************************************************************************
procedure AbreElCajon(var Impreso: TImpreso);

  function CreaLinea: String;
  var
    sAux: String;
  begin

    Result := '';

    sAux := Trim(DlgPropiedades.ComandoAperturaCajon);
    while Pos(' ', sAux) > 0 do begin

      try
        Result := Result + Chr(StrToInt(Copy(sAux, 1, Pos(' ', sAux)-1)));
      except

      end;

      sAux := TrimLeft(Copy(sAux, Pos(' ', sAux)+1, Length(sAux)));

    end;

    try
      Result := Result + Chr(StrToInt(sAux));
    except

    end;

  end;

var
  DocInfo1: TDocInfo1;
  Handle:   THandle;
  Linea:    String;
  N:        DWORD;
begin

    if not OpenPrinter(PChar(Printer.Printers[Impreso.Impresora]), Handle, nil) then begin
      ShowMessage('Error en OpenPrinter : ' + Printer.Printers[Impreso.Impresora]);
      Exit;
    end;

  end;

  with DocInfo1 do begin
    pDocName    := PChar(Impreso.Nombre);
    pOutputFile := nil;
    pDataType   := 'RAW';
  end;

  StartDocPrinter(Handle, 1, @DocInfo1);

  StartPagePrinter(Handle);

//  Linea := #27 + #112 + #0 + #127 + #127;
  Linea := CreaLinea;
  WritePrinter(Handle, PChar(Linea), Length(Linea), N);

  EndPagePrinter(Handle);

  EndDocPrinter(Handle);

  ClosePrinter(Handle);

end;

Espero que te ayude.

Saludos

josemiguelbvs 24-02-2012 16:09:00

Mas simple de lo que parece.
 
Con este sencillo ejemplo deberia funcionar en Red. Con cualquier tipo de puerto (Usb, Ethernet, LPT, o COM). Yo lo uso hace anos sin ningun problema.

La unica salvedad es que debera estar compartida tu impresora. Y luego usar una asignacion en formato UNC para imprimir.

Port := '\\Server\Impresora'; // Server=Nombre del equipo donde se conecta la impresora. Impresora=Nombre de la impresora compartida.


Port := '\\Server\Impresora';
AssignFile(MyFile, Port);
try
Rewrite(MyFile);
except
ShowMessage( 'Impresora conectada en puerto: ' + Port + '. No funciona.');
end;

Write(MyFile, Chr(27) + chr(64)); // reseteo impresora;
...
...

Writeln(MyFile, 'Imprimo algo...' );
...
...

Write(MyFile, chr(29) + chr(86)+ chr(49)); // Corta Papel


Espero, te sirva.

Saludos

Jose Miguel Bakulic V.

ethangio 24-02-2012 17:15:17

GRACIAS NEWTRON , JOSEMIGUELBVS ..

Voy a hacer algunas pruebas :D --- Gracias nuevamente ...

ethangio 29-02-2012 02:28:59

:( Lamento responder hasta ahorita pero desde el dia de la respuesta ya no tengo donde hacer pruebas de corte de papel, me iban a prestar otra pero no se cuando :( , confio en que sus soluciones que me proporcionaron funcionan .. --- Y regresaba a agradecerles por su ayuda ...

GRACIAS !!

En cuanto tenga donde hacer pruebas y heche andar ese codigo les comento ... GRACIAS nuevamente ... :D

MAXIUM 29-02-2012 02:49:01

Si lo anterior no sirve, proba un código en CMD primero:

net use LPT1 \\servidor\Okidata /persistent:yes

aunque en tu caso debiera resultar esto otro

net use COM3 \\servidor\Okidata /persistent:yes

Nota: Okidata es el nombre del recurso, es decir, el nombre la impresora. En tu caso debiera ser distinto.


La franja horaria es GMT +2. Ahora son las 20:08:53.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi