Home Devices
New Blog Post: Email: Multi-Service

Infinite loop when trying to print image as PDF

I am making a report for displaying images, as well as printing them.
I have managed to make my report's paper fit the size of the images chosen by the user dynamically, but the problem comes when I try to print the image as a PDF.

At my first try, a PDF with the image was generated, but that PDF had a blank space and the image was cut out. I figured it is due to the printer attempting to print the image with the image's size, and this size might not be printable. So I am trying to convert the size of the image to A4 only when the user confirms the print dialog and change it back to its natural size once the printing is finished.

But I am cracking my head trying to find out how to do that. Here is what I have so far:

procedure TfrmShowAnexo.Cria(anexo: TMemoryStream);
begin
frmShowAnexo := TfrmShowAnexo.Create(Self);
Self := frmShowAnexo;

ppRpt.PreviewFormSettings.PageDisplay := pdSingle;
ppRpt.PreviewFormSettings.ZoomSetting := zsWholePage;
ppRpt.PreviewFormSettings.WindowState := wsMaximized;
ppRpt.PreviewFormSettings.PageSeparation := 10;
ppRpt.SavePrinterSetup := false;
ppRpt.TextSearchSettings.Enabled := True;
ppRpt.ThumbnailSettings.Enabled := false;
ppRpt.ThumbnailSettings.Visible := false;
ppRpt.OutlineSettings.visible := False;
ppRpt.Units := utScreenPixels;

try
if IsBitmap(anexo) then
begin
oBmp := TBitmap.Create;
oBmp.LoadFromStream(anexo);
imgAnexo.Picture.Graphic := oBmp;
freeAndNil(oBmp);
end
else if IsPNG(anexo) then
begin
oPng := TPngImage.Create;
oPng.LoadFromStream(anexo);
if oPng.Width >= oPng.Height then
ppRpt.PrinterSetup.Orientation := poLandscape
else
ppRpt.PrinterSetup.Orientation := poPortrait;

ppRpt.PrinterSetup.PaperWidth := oPng.Width;
ppRpt.PrinterSetup.PaperHeight := oPng.Height;
anexoHeader.Height := oPng.Height;
imgAnexo.Width := oPng.Width;
imgAnexo.Height := oPng.Height;
imgAnexo.Picture.Graphic := oPng;
freeAndNil(oPng);
end
else if IsJPEG(anexo) then
begin
oJpg := TJpegImage.Create;
oJpg.LoadFromStream(anexo);
imgAnexo.Picture.Graphic := oJpg;
freeAndNil(oJpg);
end;
ppRpt.Print;
except on e:exception do
ShowMessage('Erro na exibição!');
end;
end;

Cria() configures some preview settings, atributes the chosen image (anexo) to my ppImage component (imgAnexo) and shows the report on screen. Up to this point, everything works fine.

procedure TfrmShowAnexo.ppRptPrintDialogClose(Sender: TObject);
begin
if ppRpt.PrintDialog.ModalResult = mrOk then
begin
ppRpt.PrinterSetup.PaperHeight := 3508;{Altura de folha A4}
ppRpt.PrinterSetup.PaperWidth := 2480;{Largura de folha A4}
anexoHeader.Height := ppRpt.PrinterSetup.PaperHeight;
imgAnexo.Width := ppRpt.PrinterSetup.PaperWidth;
imgAnexo.Height := ppRpt.PrinterSetup.PaperHeight;
imgAnexo.Picture.Graphic := oPng;
ppRpt.device := dvPrinter;
end;
end;

The problem starts at PrintDialogClose. The report doesn't show any visual changes when I set the paper size to A4, so I don't know if that change really happened. And worst, after I choose a directory to generate the PDF, the printer gets stuck in an infinite loop. What can I do?
DM-150407-Ent1700-001
Sign In or Register to comment.