Jump to content
Sign in to follow this  
Bas van der Linden

Get pdf from form/controls using skia

Recommended Posts

I'm trying to generate a pdf from a form/controls with Skia. 

I'm able to do something like this

procedure TForm1.SaveControlAsPDF(AControl: TControl; const AFileName: string);
var
  LBitmap: TBitmap;
  LDocumentStream: TFileStream;
  LDocument: ISkDocument;
  LCanvas: ISkCanvas;
  LSize: TSizeF;
  LImage: ISkImage;
begin
  LBitmap := AControl.MakeScreenshot;

  try
    LSize := TSizeF.Create(LBitmap.Width, LBitmap.Height);

    LDocumentStream := TFileStream.Create(AFileName, fmCreate);
    try
      LDocument := TSkDocument.MakePDF(LDocumentStream);
      try
        LCanvas := LDocument.BeginPage(LSize.Width, LSize.Height);
        try
          LImage := LBitmap.ToSkImage;
          if Assigned(LImage) then
            LCanvas.DrawImage(LImage, 0, 0);

        finally
          LDocument.EndPage;
        end;
      finally
        LDocument.Close;
      end;
    finally
      LDocumentStream.Free;
    end;
  finally
    LBitmap.Free;
  end;
end;

But since we use DrawImage text and other stuff won't be properly selectable. 

What I'd like to be able to do is take the LCanvas and do something like this:

AControl.PaintTo(LCanvas, AControl.LocalRect);

So it paints the control to the canvas and after LDocument.EndPage we have pdf with properly painted control, but this doesn't work because it expects TCanvas so you get: Incompatible Types: 'TCanvas' and 'ISkCanvas'.

Found solution, will update later

Edited by Bas van der Linden

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
Sign in to follow this  

×