Home Devices
New Blog Posts: Merging Reports - Part 1 and Part 2

saving a report to file, setting openfile to true

edited August 2016 in Devices
Hi

we have our own custom preview, and the following button handler (I
added some quick export buttons for PDF, CSV, XLS export) code almost
does what's required, which is to print to a file type- BUT if I set
report.openFile := true (commented out below) then the output file is
zero/severeley truncated and usually won't open successfully in the
designated app... it appears that its trying to open the file too
soon while its still writing it. If I leave openFile false then the
disk report generate fine.

If I print it via the normal print dialog with save to file checked
etc it works OK and opens OK BTW. (I have tried a little "while
report.printing do sleep(100)" loop between print and afterprint as
well)

RB Enterprize 17.02, delphi 10.1, win10

Thanks,
Chris

code follows ("report" in this context is a tppProducer) :
:: procedure TRiemannPreview.SaveReportToFile(const in_filterIndex:integer; const in_default_ext:string);
:: var
:: saveDlg : TSavedialog;
:: Ext: string;
:: begin
:: saveDlg := TsaveDialog.Create(nil);
:: try
:: saveDlg.Options := saveDlg.Options+ [ofOverwritePrompt];
:: saveDlg.Title:='Export Report to File';
:: saveDlg.Filter:=
:: 'Extensible Markup Language (XML)|*.xml|' //1
:: +'Comma Separated value (CSV)|*.csv|' //2
:: +'Excel Spreadsheet (XLS)|*.xls|' //3
:: +'Portable Document Format (PDF)|*.pdf|' //4
:: +'Word Document (DOC)|*.doc|' //5
:: +'Rich Text Format|*.rtf|' //6
:: +'Hypertext markup (HTML)|*.htm;*.html|' //7
:: +'Image (JPEG)|*.jpg;*.jpeg|' //8
:: +'Image (PNG)|*.png'; //9
:: saveDlg.FilterIndex := in_FilterIndex;
:: savedlg.DefaultExt := in_default_ext;
:: if not saveDlg.execute then exit;
::
:: Ext := extractFileExt(saveDlg.FileName);
::
:: if sameText(Ext, '.XML') then
:: report.DeviceType := dtXMLFile
:: else if sameText(Ext, '.CSV') then
:: begin
:: report.DeviceType := dtTextFile;
:: report.TextFileType := ftComma;
:: // report.openFile := true; !!!!!!!!!!!!!!!!! uncommenting these seem to interrupt the output, looks like maybe opening it before its finished wrting?
:: end
:: else if sameText(Ext, '.XLS') then
:: begin
:: report.DeviceType := dtXLSData;
:: // report.openFile := true;
:: end
:: else if sameText(Ext, '.PDF') then
:: begin
:: report.DeviceType := dtPDF;
:: // report.openFile := true;
:: end
:: else if sameText(Ext, '.DOC') then
:: report.DeviceType := dtDOC
:: else if sameText(Ext, '.RTF') then
:: report.DeviceType := dtRTF
:: else if sameText(Ext, '.HTM') Or sameText(Ext, '.HTML') then
:: report.DeviceType := dtHTMLFile
:: else if sameText(Ext, '.JPG') Or sameText(Ext, '.JPEG') then
:: report.DeviceType := dtJPEG
:: else if sameText(Ext, '.PNG') then
:: report.DeviceType := dtPNG;
::
::
:: report.ShowAutoSearchDialog := false;
:: report.TextFileName := savedlg.filename;
:: report.ShowPrintDialog := false;
:: report.ShowCancelDialog := True;// shows print progress
:: report.BackgroundPrintSettings.Enabled:=False;
:: report.BackgroundPrintSettings.Active:=False;
::
:: if (Assigned(BeforePrint)) then BeforePrint(Self);
:: try
:: report.Print;
:: finally
:: if (Assigned(AfterPrint)) then AfterPrint(Self);
:: end;
::
:: finally
:: saveDlg.Free;
:: report.ShowAutoSearchDialog := true;
:: report.TextFileName := '';
:: report.ShowPrintDialog := True;
:: report.ShowCancelDialog := True;
:: report.DeviceType := dtScreen;
:: end;
:: end;

Comments

  • edited August 2016
    Hi Chris,

    It is necessary to disconnect the screen device before
    printing/exporting a report from the previewer. See the TppViewer.Print
    for how this is done in ReportBuilder (and why it works when you use the
    Print button).

    At the top of your routine, add the following line of code:

    Viewer.ScreenDevice.Publisher := nil; //Disconnect Screen Device before
    exporting to file

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited August 2016
    Thanks again Nico, problem solved.
This discussion has been closed.