Lazarus

Programming => LCL => Topic started by: kusana on July 30, 2018, 11:37:06 pm

Title: fppdf image rotation
Post by: kusana on July 30, 2018, 11:37:06 pm
Hello,
I need to create a 2 pages PDF document, first page must be landscape and second page must be portrait... on the first landscape page I have to write an horizontal JPG, and on the second portrait page I have to write a vertical JPG image.

the problem is that I can't write a 90° rotated jpg...

Document is created but second page is not rendered by Acroabt Reader and an error message is displayed opening the PDF (a problem occours on this page, page shoud not be displayed properly, contact the author of the PDF document to fix the problem).

This is the piece of code I'm using:

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
  Image: TMemoryStream;
  jpegDecompressor: tjhandle;
  jpegWidth: cint;
  jpegHeight: cint;
  jpegSubsamp: cint;
  jpegColorspace: cint;
begin
  Image := TMemoryStream.Create;

  FDocument := TPDFDocument.Create(Nil);
  FDocument.Infos.Title := Application.Title;
  FDocument.Infos.Author := 'TEST';
  FDocument.Infos.Producer := 'TEST';
  FDocument.Infos.ApplicationName := ApplicationName;
  FDocument.Infos.CreationDate := Now;

  FDocument.Options := [poPageOriginAtTop{, poCompressImages}];

  FDocument.StartDocument;

  FSection := FDocument.Sections.AddSection; // we always need at least one section

  FPage := FDocument.Pages.AddPage;
  FPage.PaperType := ptA4;
  FPage.UnitOfMeasure := uomMillimeters;
  FPage.Orientation := ppoLandscape;
  FSection.AddPage(FPage);

  Image.LoadFromFile('100_6625.JPG');
  jpegDecompressor := tjInitDecompress();
  tjDecompressHeader3(jpegDecompressor, Image.Memory, Image.Size, @jpegWidth, @jpegHeight, @jpegSubsamp, @jpegColorspace);
  tjDestroy(jpegDecompressor);
  i := FDocument.Images.AddJPEGStream(Image, jpegWidth, jpegHeight);
  FPage.DrawImage(10, 200, 277.0, 190.0, i);

  FPage := FDocument.Pages.AddPage;
  FPage.PaperType := ptA4;
  FPage.UnitOfMeasure := uomMillimeters;
  FPage.Orientation := ppoPortrait;
  FSection.AddPage(FPage);

  Image.Clear;
  Image.LoadFromFile('100_6626.JPG');
  jpegDecompressor := tjInitDecompress();
  tjDecompressHeader3(jpegDecompressor, Image.Memory, Image.Size, @jpegWidth, @jpegHeight, @jpegSubsamp, @jpegColorspace);
  tjDestroy(jpegDecompressor);
  i := FDocument.Images.AddJPEGStream(Image, jpegWidth, jpegHeight);
  FPage.DrawImage(10, 287, 190.0, 277.0, i, 90); // <------ 90° rotation generates a wrong PDF file

  FDocument.SaveToFile('test.pdf');

  Image.Free;

  Application.Terminate;
end;

Title: Re: fppdf image rotation
Post by: kusana on August 01, 2018, 03:17:16 pm
DefaultFormatSettings.DecimalSeparator := '.';

fixed the issue, I think it's a FCL-PDF bug bacause the library should not consider local settings.
Title: Re: fppdf image rotation
Post by: wp on August 01, 2018, 07:17:54 pm
I wrote a patch and reported the issue to BugTracker: https://bugs.freepascal.org/view.php?id=34077

[EDIT] Fixed in r39542.
Title: Re: fppdf image rotation
Post by: kusana on August 04, 2018, 10:13:23 am
Thank you for the fix!
Title: Re: fppdf image rotation
Post by: kusana on August 04, 2018, 03:07:32 pm
FormatFloat is still generating wrong values for negative numbers... exponential notation is used in some cases and it generates wrong pdf files...

'0.###' or '0.###;0.###;0' should be used instead of '0.###;;0'
Title: Re: fppdf image rotation
Post by: wp on August 06, 2018, 12:35:11 pm
Please be more specific. You seem to have found some issues, but without giving details how to reproduce nobody will fix it.

I searched unit fppdf for "FormatFloat" and "Format", and did not find anywhere a mask for exponential notation. So, where will the exponential notation come from?

Your second format mask ('0.###;0.###;0') is wrong because it does not add the minus sign in the second section. the correct mask would be '0.###;-0.###;0' (or, as you note, simply '0.###').

The used format masks ('0.###;;0') are a bit strange, indeed, because why should there be a special formatting with at most 3 decimal places for positive values, but not for negative values receiving standard formatting.
Title: Re: fppdf image rotation
Post by: kusana on August 06, 2018, 06:53:32 pm
Negative values receive standard formatting, and in case of small values exponent notation is used.

Look at the attached test code and result file.

Patch included, please review and apply if ok.
Title: Re: fppdf image rotation
Post by: wp on August 07, 2018, 10:33:31 am
Fixed by Michael van Canneyt in r39585
Title: Re: fppdf image rotation
Post by: kusana on August 08, 2018, 07:56:23 am
Thank you very much!
TinyPortal © 2005-2018