Recent

Author Topic: Form to PDF?  (Read 4133 times)

sierdolg

  • New Member
  • *
  • Posts: 21
Form to PDF?
« on: February 14, 2018, 01:47:12 pm »
Hello,
is there a fairly simple way to "dump" (save) the current contents of a form, i.e. labels and the values of text/memo-fields and grids, into a PDF (or HTML or RTF) document? Somehow very similar to a "screenshot"...

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Form to PDF?
« Reply #1 on: February 14, 2018, 02:15:00 pm »
You can save a form as a bitmap (or other TPicture-supported format) with the following sort of method (declared as TNotifyEvent to make it easy to hook to a button click etc.):

Code: Pascal  [Select][+][-]
  1. uses Graphics, ...;
  2.  
  3. procedure TMainForm.SaveSelfAsBitmap(Sender: TObject);
  4. var
  5.   pic: TPicture;
  6. begin
  7.   pic:=TPicture.Create;
  8.   try
  9.     pic.Bitmap.SetSize(Width, Height);
  10.     PaintTo(pic.Bitmap.Canvas, 0, 0);
  11.     pic.SaveToFile(Name + '.bmp', 'bmp');
  12.   finally
  13.     pic.Free;
  14.   end;
  15. end;

sierdolg

  • New Member
  • *
  • Posts: 21
Re: Form to PDF?
« Reply #2 on: February 19, 2018, 02:48:06 pm »
Unfortunately I myself  badly distracted attention using the keyword "screenshot", sorry for that: I rather tought of a "somehow textual" export, from which interesting snippets (lets say a grid of serial numbers) could be copied from.

Suppose you have some form in LibreOffice Base, bound to any data source. You can export forms, including your currently displayed data, into a PDF, from which values can be copied as text.

Is something similar possible with a lazarus form (or a component like a dbgrid)?

rvk

  • Hero Member
  • *****
  • Posts: 6056
Re: Form to PDF?
« Reply #3 on: February 19, 2018, 03:13:24 pm »
You could use the export-function to PDF of LazReport.

Of even go with PowerPDF and create the pdf yourself directly.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Form to PDF?
« Reply #4 on: February 19, 2018, 03:30:25 pm »
In addition to rvk's answer, there is also jpdf and last but not least fpreport.

The latter allows for direct inclusion of database reporting, either by using the designer or doing so manually.

Neither are automatically copying data from a grid though. btw: I have no idea why you keep referring to a form. afaik only 3th party pdf-extension allows for editable/scrollable views inside a pdf

RayoGlauco

  • Full Member
  • ***
  • Posts: 174
  • Beers: 1567
Re: Form to PDF?
« Reply #5 on: February 19, 2018, 03:32:34 pm »
You can create a procedure to generate a text file containing the data you want to save.

If you want to save the contents of text fields, you can search for tEdit components:

Code: Pascal  [Select][+][-]
  1.     for i := 0 to Form1.ControlCount - 1 do
  2.       if Form1.Controls[i] is tEdit then
  3.       begin
  4.         // write info
  5.         writeln(tEdit(Form1.Controls[i]).Name + ' - ' + tEdit(Form1.Controls[i]).Text);
  6.       end;

If you want to save the contents of tStringGrid:

Code: Pascal  [Select][+][-]
  1.     for i := 0 to Form1.ControlCount - 1 do
  2.       if Form1.Controls[i] is tStringGrid then
  3.         begin
  4.           // write component name
  5.           writeln(tStringGrid(Form1.Controls[i]).Name);
  6.           // write info of each cell
  7.           for j:=0 to tStringGrid(Form1.Controls[i]).RowCount - 1 do
  8.             for k:=0 to tStringGrid(Form1.Controls[i]).Cols.Count - 1 do
  9.               begin
  10.                 writeln(j,',',k,' - ',tStringGrid(Form1.Controls[i]).Cells[j,k]);
  11.               end;
  12.         end;

You can search for all the types of component of your interest and write their values to a file.
Warning: I have not tested the code above.
To err is human, but to really mess things up, you need a computer.

alanphys

  • Jr. Member
  • **
  • Posts: 56
Re: Form to PDF?
« Reply #6 on: July 03, 2020, 11:38:41 am »
Hi

Here is an example unit that will (very crudely) render a form to a pdf.

https://github.com/alanphys/Form2PDF

It is based on fcl-pdf.

Regards
Alanphys
Fedora 34 + KDE/QT5, Tinycore 8, Windows XP-10
https://github.com/alanphys

 

TinyPortal © 2005-2018