Jump to content
Robert Gilland

PDF to PNG and PNG to PDF

Recommended Posts

That sounds like a simple task, but turns out to be quite complex and difficult.

I would tend to use integrate external tools, which is maybe the simplest way, but also there are not many simple ones.

Probably ImageMagick, GhostScript, ...

Not sure if Pdfium can do this

Probably Debenu can do this, but is maybe not supported any longer, not sure.

https://github.com/Belval/pdf2image

https://github.com/oschwartz10612/poppler-windows

UberPdf is probably dead

 

 

 

Share this post


Link to post
57 minutes ago, Rollo62 said:

UberPdf is probably dead

Sadly, that exactly hits the point. Joe Hecht passed away some time ago.

Share this post


Link to post
5 hours ago, Uwe Raabe said:

Sadly, that exactly hits the point. Joe Hecht passed away some time ago.

Oh my, I didn't notice that, my sincere condolences :classic_sad:

So I hope he is well productive on the next level now.

Share this post


Link to post
2 hours ago, Robert Gilland said:

https://delphihtmlcomponents.com/office.html looks like it's task is to covert to HTML.

It doesn't say it can convert PDF to PNG or PNG to PDF.

PDF to PNG means that PDF will be rendered to bitmap and bitmap saved to PNG. This is possible using Office Library, it even have CreateThumbnail method that make this conversion by one line of code.

Converting PNG to PDF means embedding PNG to a new PDF file. Create one line HTML like <img scr="mypng.png"> and convert it to PDF.

Share this post


Link to post
On 8/6/2023 at 12:47 AM, Alexander Sviridenkov said:

PDF to PNG means that PDF will be rendered to bitmap and bitmap saved to PNG. This is possible using Office Library, it even have CreateThumbnail method that make this conversion by one line of code.

Converting PNG to PDF means embedding PNG to a new PDF file. Create one line HTML like <img scr="mypng.png"> and convert it to PDF.

I have now received the latest html components library, and have successfully generated a PDF from a list of PNG files (thank you). However, I cannot find anything within the html component library that will export PDF file to PNG Files.

Share this post


Link to post
uses htcanvas, htoffice, htimage, htcanvasdx, htmldraw, htcanvasgdip, 
     gdipapi, gdipobj, gdiputil; 

type
  TBytesArray = array of TBytes;

procedure PDF2PNG(const FileName: string; var PNG: TBytesArray);
var D: THtDocument;
    i: integer;
    C: THtCanvasGP;
    B: TBitmap;
    BD: THtBitmapData;
    Scene: THtCanvasScene;
    Image: TGPBitmap;
    PNGEncoder: TGUID;
    BS: TBytesStream;
    SA: IStream;
begin
  D := THtDocument.Create(THtCanvasGP.PrintCanvasClass);
  try
    { Convert PDF to HTML and parse result }
    D.Parse(THtDocumentConverter.FiletoHTML(FileName));
    { Make paged layout }
    D.GeneratePagesForPrint(210, 297);
    SetLength(PNG, D.Surface.PageCount);
    for i := 0 to D.Surface.PageCount - 1 do
    begin
      C := THtCanvasGP.Create;
      try
        B := TBitmap.Create(D.Surface.Pages[i].WidthPx, D.Surface.Pages[i].HeightPx);
        try
          { Render page }
          Scene.Init(B.Canvas.ClipRect, B.Canvas);
          C.BeginScene(Scene);
          THtPageMeta(D.Surface.Pages[i]).Meta.Play(C);
          C.EndScene;
          { Save to PNG }
          Image := TGPBitmap.Create(B.Handle, B.Palette);
          try
            GetEncoderClsid('image/png', PNGEncoder);
            BS := TBytesStream.Create(nil);
            SA := TStreamAdapterFix.Create(BS, soOwned) as IStream;
            Image.Save(SA, PNGEncoder);
            PNG[i] := Copy(BS.Bytes, 0, BS.Size);
          finally
            Image.Free
          end;
        finally
          B.Free
        end;
      finally
        C.Free
      end;
    end;
  finally
    D.Free
  end;
end;

Thank you for ordering, please use this code for conversion.

 

Share this post


Link to post
On 8/11/2023 at 11:00 PM, Alexander Sviridenkov said:

Image := TGPBitmap.Create(B.Handle, B.Palette); try GetEncoderClsid('image/png', PNGEncoder); BS := TBytesStream.Create(nil); SA := TStreamAdapterFix.Create(BS, soOwned) as IStream; Image.Save(SA, PNGEncoder); PNG[i] := Copy(BS.Bytes, 0, BS.Size); finally Image.Free end;

Thanks Alexander,

  This code is windows specific.

I am attempting to convert it to firemonkey code so it can run under linux.

 

This is what I have so far. O don't know how to replace TGPBitmap to a firemonkey equivalent.

var
  PrintDoc    : THtDocument;
  li,
  liPageCount : Integer;
  locanvas    : THtCanvasFMX;
  loBitMap    : TBitmap;
  loScene     : THtCanvasScene;
  loRect      : TRect;
  loImage     : TGPBitmap;

begin
  PrintDoc := THtDocument.Create(THtCanvasFMX.PrintCanvasClass);
//     Convert PDF to HTML and parse result
  PrintDoc.Parse(THtDocumentConverter.FiletoHTML(psPDFFile));
//     Make paged layout
  if( LandScape )then
    PrintDoc.GeneratePagesForPrint(297,210)
  else
    PrintDoc.GeneratePagesForPrint(210, 297);
//    SetLength(PNG, D.Surface.PageCount);
  FLoadPNGs.Clear;
  for li := 0 to PrintDoc.Surface.PageCount - 1 do
    begin
      locanvas  := THtCanvasFMX.Create;
      try
        loBitMap := TBitmap.Create(PrintDoc.Surface.Pages[li].WidthPx
                                  ,PrintDoc.Surface.Pages[li].HeightPx);
        try
//           Render page
          loRect.Create(0,0,PrintDoc.Surface.Pages[li].WidthPx
                           ,PrintDoc.Surface.Pages[li].HeightPx);
          loScene.Init(loRect, loBitMap.Canvas);
          locanvas.BeginScene(loScene);
          THtPageMeta(PrintDoc.Surface.Pages[li]).Meta.Play(locanvas);
          locanvas.EndScene;
//           Save to PNG
          loImage := TGPBitmap.Create(loBitMap.Handle, loBitMap.Palette);
          try
            GetEncoderClsid('image/png', PNGEncoder);
            BS := TBytesStream.Create(nil);
            SA := TStreamAdapterFix.Create(BS, soOwned) as IStream;
            Image.Save(SA, PNGEncoder);
            PNG[i] := Copy(BS.Bytes, 0, BS.Size);
          finally
            Image.Free
          end;
        finally
          B.Free
        end;
      finally
        C.Free
      end

 

Share this post


Link to post

Please use the following code

 

            ST := TBytesStream.Create;
            try
              Surf := TBitmapSurface.Create;
              try
                Surf.Assign(B);
                if TBitmapCodecManager.SaveToStream(ST, Surf, '.png') then
                  PNG[i] := copy(ST.Bytes, 0, ST.Size)
                else
                  PNG[i] := nil;
              finally
                Surf.Free
              end;
            finally
              ST.Free
            end;

 

Share this post


Link to post
5 minutes ago, Robert Gilland said:

After all this, The SynPDF library is not compiling under Linux (ALexandria 11.3)

Yes, it is Windows only. There is direct PDF export in Office library, simply comment htsynpdf.

Share this post


Link to post

It seems to me that this library requires gnome or kde installed is that correct?

I am getting an access violation when the GtkWindowNew function is called, so I am guessing the Linux box needs a GUI installed to run this code.

Is that right?

    LibGDKHandle := LoadLibrary(PChar(LIB_GDK));
    if LibGDKHandle = 0 then
      raise Exception.Create('libgdk-3 not found.');
    GdkAtomIntern := GetProcAddress(LibGDKHandle, 'gdk_atom_intern');

    LibGLibHandle := LoadLibrary(PChar(LIB_GLIB));
    if LibGLibHandle = 0 then
      raise Exception.Create('GLIB 2 is required to be installed.');
    GFree := GetProcAddress(LibGLibHandle, 'g_free');

    IntClipboardOwner := GtkWindowNew(0);

 

Edited by Robert Gilland
clarification

Share this post


Link to post
On 9/13/2023 at 8:21 PM, Alexander Sviridenkov said:

Please try with the unit sent to email.

We are trying to run this code as a linux service and the service is freezing.

Can this code work under linux service conditions?

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×