#1 2018-12-19 17:13:02

swestner
Member
Registered: 2011-07-11
Posts: 8

Out of memory when converting TIFF to PDF / A

Hello,

I try to convert TIFF to PDF/A.

The code is very simple:

procedure Tiff2Pdf(const aTifFileList: TStrings; const aPdfFileName: string);
var
  i, j: Integer;
  s: AnsiString;
  pdfDoc: TPdfDocument;
  pdfpage: TPdfpage;
  pdfImage: TPdfImage;
  tifImage: TTIFFImage;
begin
  pdfDoc := TPdfDocument.Create(False, 0, True, Nil);
  try
    pdfDoc.Info.Author:='';
    pdfDoc.Info.Creator:='';
    pdfDoc.DefaultPaperSize:=psA4;
    pdfDoc.ForceJPEGCompression:=0;
    pdfDoc.CompressionMethod:=cmFlateDecode;

    tifImage := TTIFFImage.Create;

    for i := 0 to aTifFileList.Count - 1 do begin
      tifImage.LoadFromFile( aTifFileList[ i ] );
      for j := 0 to tifImage.GetPageCount - 1 do begin
        pdfpage := pdfDoc.AddPage;
        pdfpage.PageWidth  := Round(tifImage.Width *0.73);
        pdfpage.PageHeight := Round(tifImage.Height * 0.73);
        pdfImage := TPdfImage.Create(pdfDoc, tifImage, True);

        s := AnsiString( Format( 'image_%.2d_%.2d', [i, j] ) );
        pdfDoc.AddXObject( s, pdfimage );
        pdfDoc.Canvas.DrawXObject( 0, 0, pdfpage.PageWidth, pdfpage.PageHeight, s );
      end;
    end;

    pdfDoc.SaveToFile( aPdfFileName );
  finally
    pdfDoc.Free;
  end;
end;

After the 4th page I get an "Out of Memory"-exception.

The whole demo-project is located under:
https://www.kic-software.de/Download/TestTif2Pdf.zip

Any ideas how to convert large TIFFs to PDF/A using the Synopse-library?

Greetings

Stefan

Offline

#2 2018-12-20 06:12:54

Chaa
Member
Registered: 2011-03-26
Posts: 244

Re: Out of memory when converting TIFF to PDF / A

You can use SaveToStreamDirectBegin/SaveToStreamDirectPageFlush/SaveToStreamDirectEnd to write PDF page by page and reduce memory usage.

Also, to create PDF of smaller size, you can use CreateOrGetImage instead of TPdfImage.Create and set ForceJPEGCompression.

Offline

#3 2018-12-20 13:33:16

swestner
Member
Registered: 2011-07-11
Posts: 8

Re: Out of memory when converting TIFF to PDF / A

Hello,

thanks for the hint. Now memory comsumption is around 300 MB which is ok.

But the resulting PDF/A is not PDF/A-compliant.

Checking with Acrobat it says:
PDF-Document doesn't fit the PDF/A-1a(2005)-Standard.
Structured PDf-File:"Type"-entry is missing

How to fix that?

Greetings

Stefan

Offline

Board footer

Powered by FluxBB