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

Saving to PDF file from within viewer

edited February 2012 in General
Hi,

I have a custom previewer (rbprvdlg.pas) where I would like to be able
to save the report to a pdf file.

I have tried the following script, but it seems to output nothing:

ppViewer1.Report.AllowPrintToFile := true;
ppViewer1.Report.showprintdialog := true ;
ppViewer1.Report.PDFSettings.OpenPDFFile := true;
ppViewer1.Report.DeviceType := dtPDF;
ppViewer1.Report.TextFileName := savedialog1.FileName;
ppViewer1.Report.Print;


It runs through the procedure. I can see printing page 1 of 2, and then
2 of 2 show up but the file it creates doesn't open up in a PDF viewer
and appears to have no content (compared to the actual document).

Is it possible to export / create a PDF file from within the previewer?

The actual file that is created is as follows:

---------------------------
%PDF-1.4
%????
1 0 obj
<< /Creator <>
/CreationDate <443a3230313230323235313534323037313027303027>
/Title <>
/Author <>
/Producer <5265706f72744275696c646572>
/Keywords <>
/Subject <> >>
endobj
2 0 obj
[/PDF /Text /ImageB /ImageC]
endobj
3 0 obj
<< /Type /ExtGState
/SA true
endobj
4 0 obj
<< /Type /Catalog
/Pages 5 0 R
endobj
---------------------------

Thanks & Regards

Adam.

Comments

  • edited February 2012
    Sorry, I should add I'm using Delphi 2007 and Reportbuilder 14.03

    Thanks & Regards

    Adam.

  • edited February 2012
    Hi Adam,

    This is not a known issue. If you try to print to PDF using the
    built-in preview (or directly) do you get the same behavior? Are you
    trying to export to PDF and generate to screen at the same time?

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited February 2012
    Hi Nico,

    Thanks for your reply. I am trying to create the PDF file from within a
    previewer (on the screen).

    Effectively I print to screen. I then want to be able to allow the user
    to save what they're seeing on the screen to PDF from that preview window.

    Thanks & Regards

    Adam.


  • edited February 2012
    Hi Adam,

    Yes but you mention that you are using a custom preview. If you try
    exporting to PDF using the non-custom (built-in) preview, do you still
    get the same behavior?

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited March 2012
    Hi Nico,

    Sorry - I'm a little confused. When I print to preview (screen) on the
    built in one, I don't see an option that then allows me to save to file.

    I can save to PDF fine from outside of the preview screen - but what I'm
    wanting to do is save while the user has a preview screen up.

    Can you please advise me if I'm missing something? Is there a save to
    PDF option in the default previewer I'm not aware of?

    Thanks & Regards

    Adam.
  • edited March 2012
    Hi Adam,

    If you set the Report.AllowPrintToFile and ShowPrintDialog properties to
    True and preview the report, you can export to PDF by selecting the
    Print button. Then from the print dialog, you can use the bottom "print
    to file" portion to export the report.

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited March 2012
    Thanks Nico,

    That method works fine. What I am looking for is a more automated
    option. (ie, a separate button that just brings up a Save As Dialog) to
    simplify it for the users. (Make it more like what they are used to in
    other applications).

    I can do this if I use Gnostice - but can't seem to find a way to do it
    without.

    Best Regards

    Adam.
  • edited March 2012
    Hi Adam,

    Thanks, looking back at your old post I now see what is happening. In
    order to print from the preview/viewer, the screen device first needs to
    be disconnected so the onscreen report is not regenerated. In your code
    you are calling Viewer.Report.Print which does not perform this task.
    Try calling Viewer.Print instead and see if that solves the problem.

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited March 2012
    Thanks again for your reply Nico,

    Still no luck. Viewer.Print allows me to call print - but it just prints
    to the default printer. It seems to be ignoring the changes I've made to
    print to pdf instead.

    Just to confirm, is this the script that I should have?

    ppViewer1.Report.AllowPrintToFile := true;
    ppViewer1.Report.showprintdialog := true ;
    ppViewer1.Report.PDFSettings.OpenPDFFile := true;
    ppViewer1.Report.DeviceType := dtPDF;
    ppViewer1.Report.TextFileName := savedialog1.FileName;
    ppViewer1.Print;

    I tried "ppViewer1.AllowPrintToFile := true" but there is no such
    property for the viewer.

    Thanks & Regards

    Adam.
  • edited March 2012
    Hi Adam,

    In this case you would need to do something similar to what is coded in
    the Viewer.Print method (only assigning a different DeviceType).

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited March 2012
    Hi Nico,

    Thanks again for your reply.

    Sorry - but I'm completely lost now. I've tried to replicate the same
    code on my form. At present I have the following script - but it will
    not compile. I'm a little out of my depth now.

    It's not aware of what the Producer is. (Doesn't look to be a property
    of ppViewer1). Am I on the right track, or have I completely missed what
    you are suggesting?

    procedure TmySearchPrintPreview.SButtonSaveClick(Sender: TObject);
    begin
    with ppViewer1 do
    begin
    if ScreenDevice.Busy then Exit;
    if (Producer <> nil) then
    begin
    {detach screen device}
    ScreenDevice.Publisher := nil;

    {set current page}
    if ScreenDevice.AbsolutePageNo = 0 then
    Producer.CurrentPage := 1
    else
    Producer.CurrentPage := ScreenDevice.AbsolutePageNo;

    {print the report}
    SaveDevice := dtPDF; // FProducer.DeviceType;
    Producer.AddEventNotify(FWalkieTalkie);

    Producer.ShowAutoSearchDialog := False;
    Producer.DeviceType := dtpdf; // TppPrinterDevice.DeviceName;
    Producer.PrintWithSameParameters;
    end;
    end;
    end;

    Thanks & Regards

    Adam.



  • edited March 2012
    Hi Adam,

    TppProducer is an ancestor to the report. Try using the report in place
    of FProducer.

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited March 2012
    Hi Nico,

    Thanks again for your help.

    I think I'm getting somewhere now. where.

    I've replaced Producer with PPViewer1.Report

    The following 2 lines do not compile:

    SaveDevice := dtPDF; // FProducer.DeviceType;
    Producer.AddEventNotify(FWalkieTalkie);

    The first line refers to the FProducer.DeviceType. Since I have the line:

    ppViewer1.Report.DeviceType := dtpdf;

    A couple of lines down, I figured I could comment this one out.

    The second one looks like it just raises an event. As I'm not worried
    about events (and wanted to get this to compile) I removed this one too.

    I also noticed I needed to make some other adjustment / changes to the
    report to get it to print to PDF but I think it's working now.

    Here is the full code that I have that compiles, and saves a PDF File
    from the preview, just incase it is of any help to anyone else:

    procedure TrbPrintPreview.Button1Click(Sender: TObject);
    begin
    if not savedialog1.execute then
    exit;
    with ppViewer1 do
    begin
    if ScreenDevice.Busy then Exit;
    if (ppViewer1.Report <> nil) then
    begin
    {detach screen device}
    ScreenDevice.Publisher := nil;

    {set current page}
    if ScreenDevice.AbsolutePageNo = 0 then
    ppViewer1.Report.CurrentPage := 1
    else
    ppViewer1.Report.CurrentPage := ScreenDevice.AbsolutePageNo;

    ppViewer1.Report.AllowPrintToFile := true;
    ppViewer1.Report.showprintdialog := false;
    ppViewer1.Report.PDFSettings.OpenPDFFile := true;
    ppViewer1.Report.DeviceType := dtPDF;
    ppViewer1.Report.TextFileName := savedialog1.FileName;
    ppViewer1.Report.ShowAutoSearchDialog := False;
    ppViewer1.Report.DeviceType := dtpdf; // Replaces SaveDevice?
    TppPrinterDevice.DeviceName;
    ppViewer1.Report.PrintWithSameParameters;
    end;
    end;

    end;





  • edited March 2012
    Excellent, glad you got it working.

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
This discussion has been closed.