Re: printing on win32



I tried your code and that seem to work but as i'm using a virtual PDF printer it shows me a dialogbox "saying" that file I try to load is not a postcript. It seem that i've forgot something. With what sort of file did you try?

In you code there is not selection of printer, do you have something for doing it too?

Mick



2006/4/10, Claudio Polegato Junior < cpolegatojr_lsts pop com br>:
Ramashish Baranwal escreveu:
> Write a wrapper API that expose a common interface and hides
> windows/linux API under it, e.g.
>
> class Printer
> {
>     // ...
>     void print(Document doc);
> };
>
> void Printer::print(Document doc)
> {
> #if defined(WIN32)
>     // print using Windows API
> #else
>     // use linux API
> #endif
> }
>
> Well, its not going to be that simple. But I hope u got the idea.
>
> Ram
>
> On 4/8/06, Mickael Drean <mickael drean gmail com> wrote:
>
>> Well, i don't know how to do it!!  and as I'm programming a software which
>> will be available on linux and windows so using Windows API won't be a good
>> solution.
>>
>>

>> 2006/4/7, Jonathon Jongsma < jonathon jongsma gmail com>:
>>
>> On 4/7/06, Mickael Drean < mickael drean gmail com> wrote:
>>
>>> Well if cairo is able to print, does cairomm too?
>>>
>>>  thank for the informaton.
>>>
>>>  Mick
>>>
>> Cairo doesn't 'print' per se.  It's capable (or will be capable) of
>> outputting graphics in a format (i.e. postscript) that can be sent to
>> a printer.  Postscript output is still experimental, however, and it
>> won't be fully supported until the 1.2 release (supposedly coming out
>>  in the next month or so).  But cairo doesn't provide a way for you to
>> actually get the postscript output to the printer.  For that you still
>> need a printing framework (i.e. the windows API or the
>> under-development GTK printing API that murray mentioned earlier).
>>
>> Jonner
>>
>
Hi,

  You can do this:

includes:

#ifdef __WIN32__
#  include <windows.h>
#else
#  include <unistd.h>
#  include <sys/types.h>
#  include <sys/wait.h>
//#  include <cups/cups.h>  // If you are using cups
#endif


function:

    #ifdef __WIN32__
      HANDLE phPrinter;
      BOOL Result = OpenPrinter((CHAR*)Impressora.c_str(), &phPrinter,
NULL);
      if (!Result){
        debug
        throw "Erro ao abrir impressora: "+Impressora+".\nEstá sendo
utilizado o driver: "+Driver+".";
      }
      DWORD Saida = 0;
      DOC_INFO_1 di;
      di.pDocName = "Trabalho duro!";
      di.pOutputFile = (LPTSTR) NULL;
      di.pDatatype = (LPTSTR) NULL;
      Result = StartDocPrinter(phPrinter, 1, (LPBYTE)&di);
      if (!Result){
        ClosePrinter(phPrinter);
        throw "Erro ao iniciar o documento na impessora:
"+Impressora+".\nEstá sendo utilizado o driver: "+Driver+".";
      }
      Result = StartPagePrinter(phPrinter);
      if (!Result){
        EndDocPrinter(phPrinter);
        ClosePrinter(phPrinter);
        throw "Erro ao iniciar a página na impessora:
"+Impressora+".\nEstá sendo utilizado o driver: "+Driver+".";
      }

      std::ifstream is;

is.open((getenv("TMP")+Glib::ustring("/Arquivo_Impressão")).c_str(),
std::ios::in | std::ios::binary);
      if (!is){
        throw  "Erro ao carregar dados para a impessora:
"+Impressora+".\nEstá sendo utilizado o driver: "+Driver+".";
      }

      // get length of file:
      is.seekg(0, std::ios::end);
      DWORD length = is.tellg();
      is.seekg (0, std::ios::beg);

      // allocate memory:
      if (length == -1u)
        throw  "Erro ao carregar dados para a impessora:
"+Impressora+".\nEstá sendo utilizado o driver: "+Driver+".";
      char *buffer = new char [length];

      // read data as a block:
      is.read (buffer,length);

      is.close();

      Result = WritePrinter(phPrinter, buffer, length, &Saida);
      if (!Result){
        delete buffer;
        EndPagePrinter(phPrinter);
        EndDocPrinter(phPrinter);
        ClosePrinter(phPrinter);
        throw "Erro ao imprimir o texto na impessora:
"+Impressora+".\nEstá sendo utilizado o driver: "+Driver+".";
      }
      if (Saida != length){
        delete buffer;
        EndPagePrinter(phPrinter);
        EndDocPrinter(phPrinter);
        ClosePrinter(phPrinter);
        throw "Erro no tamanho do texto impresso na impessora:
"+Impressora+".\nEstá sendo utilizado o driver: "+Driver+".";
      }
      delete buffer;
      Result = EndPagePrinter(phPrinter);
      if (!Result){
        EndDocPrinter(phPrinter);
        ClosePrinter(phPrinter);
        throw "Erro ao encerrar a página na impessora:
"+Impressora+".\nEstá sendo utilizado o driver: "+Driver+".";
      }
      Result = EndDocPrinter(phPrinter);
      if (!Result){
        ClosePrinter(phPrinter);
        throw "Erro ao encerrar o documento na impessora:
"+Impressora+".\nEstá sendo utilizado o driver: "+Driver+".";
      }
      Result = ClosePrinter(phPrinter);
      if (!Result){
        throw "Erro ao encerrar serviços com a impressora:
"+Impressora+".\nEstá sendo utilizado o driver: "+Driver+".";
      }
    #else
      Glib::ustring Servidor = "X";
      Glib::ustring Impressora = "Y";
      Glib::ustring Comentario = "Comentário";
      if (Comentario == "")
        Comentario = "Sem Descrição";
      int erro[2];
      pipe(erro);
      pid_t Processo = fork();
      if (Processo == 0){
        close(erro[0]);
        dup2(erro[1], STDERR_FILENO);
        bool Direta = (Conteudo_Atributo(Nome_Driver, "Direta")=="Sim");
        if (Servidor == "" && Impressora == ""){
          if (Direta){
            execlp("lp", "lp", "-oraw", "-t", Comentario.c_str(),
(getenv("TMP")+Glib::ustring("/Arquivo_Impressão")).c_str(), NULL);
          }
          else{
            execlp("lp", "lp", "-t", Comentario.c_str (),
(getenv("TMP")+Glib::ustring("/Arquivo_Impressão")).c_str(), NULL);
          }
        }
        else if (Servidor == ""){
          if (Direta){
            execlp("lp", "lp", "-oraw", "-d", Impressora.c_str(), "-t",
Comentario.c_str(),
(getenv("TMP")+Glib::ustring("/Arquivo_Impressão")).c_str(), NULL);
          }
          else{
            execlp("lp", "lp", "-d", Impressora.c_str(), "-t",
Comentario.c_str(),
(getenv("TMP")+Glib::ustring("/Arquivo_Impressão")).c_str(), NULL);
          }
        }
        else if (Impressora == ""){
          if (Direta){
            execlp("lp", "lp", "-oraw", "-h", Servidor.c_str(), "-t",
Comentario.c_str(),
(getenv("TMP")+Glib::ustring("/Arquivo_Impressão")).c_str(), NULL);
          }
          else{
            execlp("lp", "lp", "-h", Servidor.c_str(), "-t",
Comentario.c_str(),
(getenv("TMP")+Glib::ustring("/Arquivo_Impressão")).c_str(), NULL);
          }
        }
        else{
          if (Direta){
            execlp("lp", "lp", "-oraw", "-h", Servidor.c_str(), "-d",
Impressora.c_str(), "-t", Comentario.c_str(),
(getenv("TMP")+Glib::ustring("/Arquivo_Impressão")).c_str(), NULL);
          }
          else{
            execlp("lp", "lp", "-h", Servidor.c_str (), "-d",
Impressora.c_str(), "-t", Comentario.c_str(),
(getenv("TMP")+Glib::ustring("/Arquivo_Impressão")).c_str(), NULL);
          }
        }
        char a[] = "Sem resposta do Sistema de Impressão";
        write(erro[1], a, sizeof(a));
        exit(-1);
      }
      close(erro[1]);
      int Situacao;
      wait(&Situacao);
      char a[1024];
      int lidos = read(erro[0], a, 1023);
      a[lidos] = 0;
      close(erro[0]);
      if (Situacao != 0 || lidos){
        throw "Erro ao tentar imprimir!\nServidor:
"+Servidor+"\nImpressora: "+Impressora+"\nDriver: "+Nome_Driver+"\nErro:
"+a;
      }
/*
      cupsSetServer(Servidor.c_str());
      int Num_Impressao;
      if (!(Num_Impressao = cupsPrintFile(Impressora.c_str(),
(getenv("TMP")+Glib::ustring("/Arquivo_Impressão")).c_str(), "Sistema de
Impressão por Claudio Polegato Junior", 0, NULL))){
        throw "Erro ao imprimir o texto na impessora:
"+Impressora+".\nEstá sendo utilizado o driver: "+Nome_Driver+".";
      }
*/

    #endif


Sorry about brazilian portuguese.

--

[]'s

Claudio Polegato Junior

Um peregrino de Problemas; Um pergaminho de Soluções.




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]