Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 10 of 12 total
Thread Displaying Fastreport as PDF
Mon, Sep 14 2015 7:09 AMPermanent Link

Godfrey

Ultimatesoft

Hi all

I have a Module to send a PDF report to my EWB client.  My code is as follows:

procedure TEWBModule1.EWBModuleExecute(Request: TEWBServerRequest);
begin
  if AuthenticateUser(Request) then
     begin
     // Tell browser not to cache response
     Request.ResponseHeaders.Add('Cache-Control: no-cache');
     case Request.RequestMethod of
        rmGet:
           begin
            if AnsiSameText(Request.RequestParams.Values['method'],'VehPDF') then
              begin
               FDQuery1.Open;
               SendReportasPDF(Request, frxReport1, frxPDFExport1);
               FDQuery1.Close;
              end
              else
               Request.SendError(HTTP_BAD_REQUEST,'Invalid request');
           end;
        // Include a HEAD response for situations where the client does not
        // specify a MIME type, and the browser needs to use a HEAD request
        // to discover the content type for the URL
        rmHead:
           begin
             if AnsiSameText(Request.RequestParams.Values['method'],'VehPDF') then
              begin
               FDQuery1.Open;
               SendReportasPDF(Request, frxReport1, frxPDFExport1);
               FDQuery1.Close;
              end
              else
               Request.SendError(HTTP_BAD_REQUEST,'Invalid request');
           end;
        else
           Request.SendError(HTTP_BAD_REQUEST,'Invalid request');
        end;
     end
  else
     Request.SendError(HTTP_BAD_REQUEST,'Authentication failed');
end;


procedure TEWBModule1.SendReportasPDF(ARequest: TEWBServerRequest; AReport : TfrxReport;
                          APDFExport: TfrxPDFExport);
var
TempStream : TMemoryStream;
begin
TempStream := TMemoryStream.Create;
try
 APDFExport.Stream := TempStream;
 AReport.Export(APDFExport);
 ARequest.SendCustomContentStream(TempStream,'application/pdf','');
finally
 FreeAndNil(TempStream);
end;
end;


Then on the client I use a TPlugin to display report as follows:

PDFPlugin.URL:='modules/pdfmodule?user=Demo&password=Password'+
                    '&method=VehPDF';

On the client when the plugin loads and I get a message
"This file cannot be opened because there are no pages"

I can view the report at design time in the module so that is working.  What am I doing wrong?

Thanks
Godfrey
Mon, Sep 14 2015 7:18 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Godfrey,

<< I can view the report at design time in the module so that is working.  What am I doing wrong? >>

Two things stand out:

1) You're not preparing/executing your report (not entirely familiar with FastReports, but I think that's required).
2) You're not handling HEAD requests properly by using this method:

http://www.elevatesoft.com/manual?action=viewmethod&id=ewb2mod&product=rsdelphi
&version=XE&comp=TEWBServerRequest&method=SendCustomContentHeader

instead, you're sending the whole PDF over as a response, which is a no-no.

Tim Young
Elevate Software
www.elevatesoft.com
Mon, Sep 14 2015 7:35 AMPermanent Link

Godfrey

Ultimatesoft

Tim Young [Elevate Software] wrote:

<1) You're not preparing/executing your report (not entirely familiar with FastReports, but I think that's required).>

OK I forgot to Prepare the report.  Added this in and report is now showing.


<instead, you're sending the whole PDF over as a response, which is a no-no.>

What is the correct way?  I was just working from examples you sent me.

Thanks
Godfrey
Mon, Sep 14 2015 9:39 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Godfrey,

<< What is the correct way?  I was just working from examples you sent me. >>

The PDF Module example project that comes with EWB does this for HEAD requests:

        // Include a HEAD response for situations where the client does not
        // specify a MIME type, and the browser needs to use a HEAD request
        // to discover the content type for the URL
        rmHead:
           begin
           if AnsiSameText(Request.RequestParams.Values['method'],'load') then
              begin
              TempRootFileName:=Request.RequestParams.Values['name']+'.pdf';
              TempFileName:=IncludeTrailingBackslash(TempPDFDirectory)+TempRootFileName;
              if FileExists(TempFileName) then
>>>>>>>>> Request.SendCustomContentHeader('application/pdf','inline; filename="'+TempRootFileName+'"')
              else
                 Request.SendError(HTTP_BAD_REQUEST,'Specified PDF does not exist');
              end
           else
              Request.SendError(HTTP_BAD_REQUEST,'Invalid request');
           end;

....

Tim Young
Elevate Software
www.elevatesoft.com
Tue, Sep 15 2015 2:20 AMPermanent Link

Godfrey

Ultimatesoft

Tim Young [Elevate Software] wrote:

Godfrey,
<<The PDF Module example project that comes with EWB does this for HEAD requests:

        // Include a HEAD response for situations where the client does not
        // specify a MIME type, and the browser needs to use a HEAD request
        // to discover the content type for the URL
        rmHead:
           begin
           if AnsiSameText(Request.RequestParams.Values['method'],'load') then
              begin
              TempRootFileName:=Request.RequestParams.Values['name']+'.pdf';
              TempFileName:=IncludeTrailingBackslash(TempPDFDirectory)+TempRootFileName;
              if FileExists(TempFileName) then
>>>>>>>>> Request.SendCustomContentHeader('application/pdf','inline; filename="'+TempRootFileName+'"')
              else
                 Request.SendError(HTTP_BAD_REQUEST,'Specified PDF does not exist');
              end
           else
              Request.SendError(HTTP_BAD_REQUEST,'Invalid request');
           end;

Tim Young
Elevate Software
www.elevatesoft.com

Sorry I misunderstood your last answer.  I thought you were referring to the GET request in your last sentence.
So is my Get request is OK.  I just need to work on the head request.

Am I correct in saying the Get request sends the PDF as stream and in the Head request I need to send
the PDF as a file.  

Thanks
Godfrey
Wed, Sep 16 2015 9:13 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Godfrey,

<< Am I correct in saying the Get request sends the PDF as stream and in the Head request I need to send
the PDF as a file. >>

No, you shouldn't send *any* content in response to a HEAD request.  The sole purpose of a HEAD request is to tell the browser what type of content (and other things like when the resource expires) the referenced resource is.

http://stackoverflow.com/questions/27868314/avoiding-content-length-in-head-response

Which is what the SendCustomContentHeader does.

Tim Young
Elevate Software
www.elevatesoft.com
Mon, Apr 11 2016 3:15 AMPermanent Link

Godfrey

Ultimatesoft

<<Tim Young [Elevate Software] wrote:>>

<<Which is what the SendCustomContentHeader does.>>

I have been away for a while. Hoping to start a new project with Ver 2.05.  In the mean time I want to sort out how to display a report.

I was not successful previously, with streaming PDF reports(as per my code above) and just gave up in the end.
In the PDF example supplied with EWB, a pdf file is sent which I do not have a problem with, but I need to create the report in fastreports then export it to pdf and stream it to the client.  I cannot seem to get this right.

Is the code in my GET section above ok?

Can someone help me with the coding for the HEAD section

I had a look at SendCustomContentHeader but cannot find much about it in the manuals. When streaming the report, what do put in as the "content disposition" parameter. Can someone help me with the correct coding for the Head section, I would really like to get this right as my project cannot run without it.

Thanks
Godfrey.
Mon, Apr 11 2016 5:17 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Godfrey,

<< Is the code in my GET section above ok? >>

Yes.

<< Can someone help me with the coding for the HEAD section >>

It's the same as the SendCustomContentStream call, minus the actual stream as the first parameter:

http://www.elevatesoft.com/manual?action=viewmethod&id=ewb2mod&product=rsdelphi&version=XE&comp=TEWBServerRequest&method=SendCustomContentHeader

This is shown in the PDF module example Delphi project that ships with EWB.  Did you look at the example project ?

Tim Young
Elevate Software
www.elevatesoft.com
Mon, Apr 11 2016 7:04 AMPermanent Link

Godfrey

Ultimatesoft

Tim Young [Elevate Software] wrote:

<<It's the same as the SendCustomContentStream call, minus the actual stream as the first parameter:

This is shown in the PDF module example Delphi project that ships with EWB.  Did you look at the example project ?>>

Yes I have been looking at the PDF example which I dont have a problem with, but what I am trying to do is a bit different.  Can you please have a look at my code below.  What do I need to put in as the ContentDisposition paramter I am not sure what is required, should it be blank.

procedure TEWBModule1.EWBModuleExecute(Request: TEWBServerRequest);
begin
  if AuthenticateUser(Request) then
   begin
     Request.ResponseHeaders.Add('Cache-Control: no-cache');
     case Request.RequestMethod of
        rmGet:
           begin
            if AnsiSameText(Request.RequestParams.Values['report'],'VehPDF') then
              begin
               FDQuery1.Open;
               SendReportasPDFStream(Request, frxReport1, frxPDFExport1);
               FDQuery1.Close;
              end
              else
               Request.SendError(HTTP_BAD_REQUEST,'Invalid request');
           end;
        rmHead:
           begin
             if AnsiSameText(Request.RequestParams.Values['report'],'VehPDF') then
              begin
               FDQuery1.Open;
               SendReportasPDFHead(Request, frxReport1, frxPDFExport1);
               FDQuery1.Close;
              end
              else
               Request.SendError(HTTP_BAD_REQUEST,'Invalid request');
           end;
        else
           Request.SendError(HTTP_BAD_REQUEST,'Invalid request');
        end;
   end
  else
     Request.SendError(HTTP_BAD_REQUEST,'Authentication failed');
end;


procedure TEWBModule1.SendReportasPDFStream(ARequest: TEWBServerRequest; AReport : TfrxReport;
                          APDFExport: TfrxPDFExport);
var
TempStream : TMemoryStream;
begin
TempStream := TMemoryStream.Create;
try
 APDFExport.Stream := TempStream;
 AReport.PrepareReport();
 AReport.Export(APDFExport);
 ARequest.SendCustomContentStream(TempStream,'application/pdf','');
finally
 FreeAndNil(TempStream);
end;
end;


procedure TEWBModule1.SendReportasPDFHead(ARequest: TEWBServerRequest; AReport : TfrxReport;
                          APDFExport: TfrxPDFExport);
var
TempStream : TMemoryStream;
begin
TempStream := TMemoryStream.Create;
try
 APDFExport.Stream := TempStream;
 AReport.PrepareReport();
 AReport.Export(APDFExport);
 ARequest.SendCustomContentHeader('application/pdf','')
finally
 FreeAndNil(TempStream);
end;
end;
Mon, Apr 11 2016 7:24 AMPermanent Link

Godfrey

Ultimatesoft

Hi Tim

I tested my client now and it seems to be working.  Any improvement to my code that you can suggest would be most welcome.

Thanks
Godfrey
Page 1 of 2Next Page
Jump to Page:  1 2
Image