Recent

Author Topic: Printing in a CGI Application  (Read 6923 times)

franchus3

  • Newbie
  • Posts: 3
Printing in a CGI Application
« on: September 13, 2010, 01:21:36 pm »
Hi All,
  I'm trying to print a image. But I get the following error: "Failed to get raw image description from bitmap".

My code is:
Code: [Select]
var
  myBit, PrintBit: TFPMemoryImage;
  PrintBitCanvas: TFPImageCanvas;
  FinalCanvas: TFPImageCanvas;
  myBitCanvas: TFPImageCanvas;
  writer: TFPWriterBMP;
  imageBMP: TBitmap;
  imageToPrint: TMemoryStream;
  ltop, lleft: integer;
  dR, sR: TRect;
  dtl, dtr, dbl, dbr: TPoint;
begin
  Printer.SetPrinter(printerName);
  //get a resized copy of the bitmap :
  myBit := resizeBitmap(printerName, B);
  //create page bitmap
  PrintBit := TFPMemoryImage.Create(0,0);
  PrintBit.UsePalette:= False;
  PrintBit.Width := getPrinterPageWidth(printerName);
  PrintBit.Height := getPrinterPageHeight(printerName);
 // PrintBit.Canvas.Brush.Color := clwhite;
  //PrintBit.Canvas.Brush.Style := bssolid;
    PrintBitCanvas:=TFPImageCanvas.create(PrintBit);
  //find out where to place the bitmap to be printed in the center of our page:
  lleft := (getPrinterPageWidth(printerName) - myBit.Width) div 2;
  ltop := (getPrinterPageHeight(printerName) - myBit.Height) div 2;
  //copying and positioning bitmap on page bitmap
  sR := Rect(0, 0, myBit.Width, myBit.Height);
  dR := Rect(lleft, ltop, lleft + myBit.Width, ltop + myBit.Height);
  //PrintBit.canvas.CopyRect(dR, myBit.Canvas, sR);
  myBitCanvas:=TFPImageCanvas.create(myBit);
  PrintBitCanvas.CopyRect(lleft, ltop, myBitCanvas, sR);
  //printing outline
  dtl.x := dr.Left;
  dtl.y := dr.Top;
  dtr.x := dr.Right;
  dtr.y := dr.Top;
  dbr.x := dr.Right;
  dbr.y := dr.Bottom;
  dbl.x := dr.Left;
  dbl.y := dr.Bottom;
  with Printer do
  begin
    if not (Printer.PrinterState = psReady) then
      raise Exception.Create('La impresora no está lista');
    Printer.BeginDoc;
    Printer.Title:=job_name;
    Printer.NewPage;


    writer:= TFPWriterBMP.Create;
    imageToPrint:=TMemoryStream.Create;
   
    PrintBit.SaveToStream(imageToPrint, writer);

    imageToPrint.Position := 0;
    imageBMP:= TBitmap.Create;
    imageBMP.LoadFromStream(imageToPrint);
       
     Printer.Canvas.TextOut(10, 10, 'Hola');
     Printer.Canvas.StretchDraw(Rect(0, 0, Printer.PageWidth, Printer.PageHeight),imageBMP);
     Printer.Canvas.Draw(0,0, imageBMP);
    Printer.EndDoc;
  end;
  imageToPrint.Free;
  myBitCanvas.Free;
  PrintBit.Free;     

I didn't get any printing using the Printer.Canvas.Textout,StrechDraw or Draw. I've added Printer4Lazarus package.

I coudn't find what I'm doing wrong.  :(

I'll apreciate any guidance!

Franco

franchus3

  • Newbie
  • Posts: 3
Re: Printing in a CGI Application
« Reply #1 on: September 13, 2010, 04:56:14 pm »
I correct myself, I was using a CUPS-PDF printer and for some reason don't print Printer.Canvas,Textout. But in a physical printer it works without problems. However if I try print images, I still getting the error above.

Franco

mas steindorff

  • Sr. Member
  • ****
  • Posts: 419
Re: Printing in a CGI Application
« Reply #2 on: September 13, 2010, 08:54:06 pm »
     Printer.Canvas.TextOut(10, 10, 'Hola');
     Printer.Canvas.StretchDraw(Rect(0, 0, Printer.PageWidth, Printer.PageHeight),imageBMP);
     Printer.Canvas.Draw(0,0, imageBMP);

I use this same code with my pdf printer but without the .Draw code. my code is simply:
Code: [Select]
    printer.BeginDoc;
    Printer.Canvas.StretchDraw(r, Mybitmap); //Bitmap.Canvas.Handle;
    Printer.EndDoc;
I had errors with the draw command as well so i removed it.
windows 7/10 - laz 2.0 / 1.2.6 general releases

franchus3

  • Newbie
  • Posts: 3
Re: Printing in a CGI Application
« Reply #3 on: September 14, 2010, 01:30:59 pm »
Hi mas steindorff,
  Do you use "NoGui" fpc option compiler on your test project? I think that the problem is that I shouldn't use TBipmap, because it's a graphic component. This code works fine if I compile this like a GUI Application. I have the "Failed to get raw image description from bitmap" error, and below it says "TRasterImage.BitmapHandleNeeded: Unable to create handles, using default".
I mean, I did some changes to, for example, get width and height of jpg images because when I used graphics components, these don't work. Instead of Graphics, I use fc-image.

I'm using Laz 0.9.29 and fpc 2.5.1



 

mas steindorff

  • Sr. Member
  • ****
  • Posts: 419
Re: Printing in a CGI Application
« Reply #4 on: September 14, 2010, 06:17:33 pm »
Hi mas steindorff,
  Do you use "NoGui" fpc option compiler on your test project? I think that the problem is that I shouldn't use TBipmap, because it's a graphic component. This code works fine if I compile this like a GUI Application.

I used the code in GUI.  I just saw the draw command and remembered my woes with it.  You may look at the inheradance of a tbitmap.  It has some relations with the FCxxxx that you may be able to do some casting with.
windows 7/10 - laz 2.0 / 1.2.6 general releases

martinrame

  • Jr. Member
  • **
  • Posts: 73
Re: Printing in a CGI Application
« Reply #5 on: May 21, 2018, 02:20:38 pm »
I'm having exactly the same problem, did you find a way to print bitmaps from CGI?