Exporting a report to a PDF file using the built-in PDF device is very easy using ReportBuilder.
Exporting directly to a stream is also possible and can be useful if the output is needed in memory rather than on the hard disk. This can be done by manually assigning the TppPDFDevice.OutputStream property to a newly created TStream descendent (TMemoryStream). See the example below on how this can be done.
Use the TppReport.PDFSettings properties to manage and control the PDF file created. Some useful features include:
Download: PDFDevice.zip
Sample Delphi code:
Export PDF to file in code:
procedure TForm1.btnPrintClick(Sender: TObject); begin ppReport1.AllowPrintToFile := True; ppReport1.ShowPrintDialog := False; ppReport1.DeviceType := dtPDF; ppReport1.PDFSettings.Author := 'ReportBuilder'; ppReport1.PDFSettings.Title := 'Export to PDF Demo'; ppReport1.PDFSettings.OpenPDFFile := True; ppReport1.TextFileName := 'C:\MyPDFFile.pdf'; ppReport1.Print; end;
Export PDF to stream in code:
procedure TPDFFrm.btnGenerateClick(Sender: TObject);
var
lPDFDevice: TppPDFDevice;
begin
// delete existing file
if FileExists(ppReport1.TextFileName) then
DeleteFile(ppReport1.TextFileName);
// creat and configure the PDFDevice
lPDFDevice := TppPDFDevice.Create(nil);
if (FOutputStream = nil) then
FOutputStream := TMemoryStream.Create
else
FOutputStream.Clear;
try
lPDFDevice.PDFSettings := ppReport1.PDFSettings;
lPDFDevice.OutputStream := FOutputStream; // assign output stream
lPDFDevice.Publisher := ppReport1.Publisher;
// generate the report
ppReport1.PrintToDevices;
btnSaveToFile.Enabled := True;
finally
lPDFDevice.Free;
end;
end;