Fórum Impressão direto impressora #233166

19/05/2004

0

peguei o código abaixo na internet, mas não estou conseguindo faze-lo funcionar:

//================================================================ // Inicializa a Impressora e o documento a ser impresso // PrinterName = Nome da Impressora (Pode ser uma parte do nome ou // vazio = impressora padrão ) // Exemplo: OpenPrn(´EPSON´,´Relatorio de Clientes´) //================================================================ Function OpenPrn(PrinterName,DocumentTitle:String):Boolean; Var PName : String; I : Integer; DocInfo : TDocInfo1; dwJob : DWord; begin Result := False; If PrinterHandle<>0 Then Exit; // Evita inicialização mais de uma vez PrinterHandle := 0; // Existe impressora conectada? If Printer.Printers.Count=0 Then Begin Raise Exception.Create(´Nenhuma impressora instalada.´); Exit; End; // Se informar o nome, posiciona na impressora... // Se o nome nao for informado, usa a impressora padrão do sistema If PrinterName<>´´ Then Begin For i := 0 to Printer.Printers.Count-1 do Begin PName := Printer.Printers[i]; If Pos(AnsiUpperCase(PrinterName),AnsiUpperCase(PName))<>0 Then Break; End; End Else Begin Printer.PrinterIndex := -1; PName := Printer.Printers[Printer.PrinterIndex]; End; // Retira a informação da porta do nome da impressora. PName := TrimRight(Copy(PName,1,Pos(´ on ´,PName))); // Inicializa a impressora informada if Not OpenPrinter(PChar(PName), PrinterHandle, nil) Then begin Raise Exception.Create(SysErrorMessage(GetLastError)); Exit; end; // Inicializa o dispositivo DocInfo.pDocName := PChar(DocumentTitle); DocInfo.pOutputFile := nil; DocInfo.pDatatype := ´RAW´; // Inicia o spooler dwJob := StartDocPrinter(PrinterHandle, 1, @DocInfo); If dwJob = 0 Then begin ClosePrinter(PrinterHandle); Raise Exception.Create(SysErrorMessage(GetLastError)); Exit; end; // Inicializa a página if not StartPagePrinter(PrinterHandle) Then begin EndDocPrinter(PrinterHandle); ClosePrinter(PrinterHandle); Raise Exception.Create(SysErrorMessage(GetLastError)); Exit; end; Result := True; end; //================================================================ // Envia uma String para a impressora selecionada // Obs: Execute antes a função OpenPrn //================================================================ Function PrintString(StrToPrint:String; LineFeed:Boolean):Boolean; Var dwBytesWritten, dwCount : DWord; Begin Result := True; If PrinterHandle=0 then OpenPrn(´´,Application.Name); // Considera a impressora default // Envia a string para a impressora If LineFeed Then StrToPrint := StrToPrint+#1013; dwCount := Length(StrToPrint); if not WritePrinter(PrinterHandle,PChar(StrToPrint),dwCount,dwBytesWritten) then begin EndPagePrinter(PrinterHandle); EndDocPrinter(PrinterHandle); ClosePrinter(PrinterHandle); Raise Exception.Create(SysErrorMessage(GetLastError)); Result := False; end; End; //================================================================ // Fecha o Driver de impressão //================================================================ Function ClosePrn : Boolean; Begin Result := False; If PrinterHandle=0 then Exit; // Finaliza a página if not EndPagePrinter(PrinterHandle) Then begin EndDocPrinter(PrinterHandle); ClosePrinter(PrinterHandle); Raise Exception.Create(SysErrorMessage(GetLastError)); Exit; end; // Libera o spooler ao final da impressao if not EndDocPrinter(PrinterHandle) Then begin ClosePrinter( PrinterHandle ); Raise Exception.Create(SysErrorMessage(GetLastError)); Exit; end; // Libera o Handle ClosePrinter( PrinterHandle ); PrinterHandle := 0; Result := True; end; //Exemplo de Uso OpenPrn(´EPSON´,´Relatorio de Clientes´) PrintString(´Nome: Adenilton Rodrigues´,True); PrintString(´Telefone: 000000´,True); ClosePrn; =====================================================


Alguem sabe como usa-lo ?
Obrigado
Sanses


Sanses

Sanses

Responder

Posts

19/05/2004

Fabiano Freitas

Putz... Faz isto aqui que é muito mais simples. Vc associa a porta da impressora a um TextFile e escreve direto nela, como se fosse em DOS.

Sempre usei este conceito, desde a época que programava em Pascal e nunca tive problemas. Se seu relatório tiver mudança de tamanho de colunas , o melhor é fixar um valor máximo e utilizar a função Format para passar o texto :
Write(TextFile,Format(´¬5s´,[´coluna1´]);
Write(TextFile,Format(´¬-15s´,[coluna2´]);
e por aí vai....

E funciona com tudo que é impressora, de matricial a laser.

var
   txtFile : TextFile
begin
    Printer.Canvas.Font.Name := ´Courier New´;
    Printer.Canvas.Font.Size := 10;
    Printer.Orientation      := poPortrait;
    Printer.Title            := ´Seu Relatório´;

    AssignPrn(txtFile);
    try
      ReWrite(txtFile);
      //... código de carga - leitura de um memo, while numa tabela, etc..
      Write  ( txtFile, ´seu texto´));
      Write  ( txtFile, ´seu texto´));
    finally
      System.closeFile(txtFile);
    end;
end;



Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar