Your SAS programs, embedded in web apps and elsewhere

How to display a pdf/html/rtf file?

Accepted Solution Solved
Reply
PROC Star
Posts: 1,233
Accepted Solution

How to display a pdf/html/rtf file?

Perhaps an odd question, but...

How can I have a stored process open a static html/pdf/rtf/SASREPORT file that is sitting on the server?  By "open" I mean display the results if it is html or SASREPORT, prompt the user to open or download if it is pdf/rtf.  Process will be called from SPWA and/or WRA, on the stored process server.

Background is that I am imaging a stored process where I will:

1. Run a report and write the report to a file (html/pdf/rtf/SASREPORT), but NOT display the results to the user.

2. Scan the log from running the report.

3. If the log is clean, I will want to display the report.  If the log has errors, I will display an error message.

My first thought was to use ODS document to generate the report (without displaying in).  Then I can conditionally REPLAY the document inside of %STPBEGIN / %STPEND to display the results.

That works okay, but I lose some minor formatting which isn't captured in the document (e.g. page break info).

So I was thinking I could just use good old ods html/pdf/rtf to write a file to a temp location on the server, and then conditionally display it.

But once the file is wrtten, I'm not sure how to display it?  I think I basically want to push the file back to _webout (with the appropriate header indicating it's an attachement when it is rtf or pdf or SASREPORT).  Maybe for html it's as simple as  something like:

  data _null_;

    file _webout;

    infile "file";

    input;

    put _infile_;

  run;

?

But not sure how it would work for pdf/rtf, and Henderson's book is at the office....


Thanks,,

--Q.



Accepted Solutions
Solution
‎05-24-2012 02:20 PM
PROC Star
Posts: 1,233

Re: How to display a pdf/html/rtf file?

I think I've got streaming of a file back to _webout working.  Instead of using %filesrv for the pdf, I found a tip on serving a binary file: http://support.sas.com/kb/6/588.html  .

Below is my current code for streaming a file sitting on the server (html, rtf , pdf):

data _null_;
  file _webout;
  infile myhtml;
  input ;
  put _infile_;
run;

data _null_;
  file _webout;

  infile myrtf;

  if _n_ = 1 then do;
    rc = stpsrv_header('Content-type','application/msword');

    rc = stpsrv_header('Content-disposition',"attachment; filename=SampReplay_rtf.doc");

  end;
  input ;
  put _infile_;
run;

data _null_;
  file _webout recfm=s;
  infile  mypdf recfm=n;
  if _n_ = 1 then do;
    rc = stpsrv_header('Content-type','application/pdf');
    rc = stpsrv_header('Content-disposition',"attachment; filename=SampReplay_pdf.pdf");
  end;
  input c $char1.;
  put c $char1. @@;
run;

View solution in original post


All Replies
Contributor
Posts: 45

Re: How to display a pdf/html/rtf file?

Why would you have errors? (beyond the initial testing)...I do this with RTF, but I just stream the results back and the browser opens MS Word for the user and displays the results.

Just curious why you would have unexpected errors...

PROC Star
Posts: 1,233

Re: How to display a pdf/html/rtf file?

Hi, Jay, Thanks for replying.

I actually haven't tried this yet, so can't tell you what errors I have hit (may be posting more this afternoon...).  I assumed this method might not work with a PDF file, since it is not a text file. I would expect RTF and SASREPORT (xml) to be easier.

Looking around a bit, looks like maybe I need to use %filesrv?

  http://support.sas.com/rnd/web/intrnet/filesrv/filesrv.html

Have you seen any (simple-ish) examples of writing having one requeset that writes a rtf/pdf to somewhere on the server, and then streams that file back to the user?  

Thnaks,

--Q.


Contributor
Posts: 45

Re: How to display a pdf/html/rtf file?

Quentin,

Why do you need to store the file on the server? Why not just stream it to the user....

PROC Star
Posts: 1,233

Re: How to display a pdf/html/rtf file?

Jay,

As described in my first post, I'm trying to set up a process where when a user submits a stored process to generate a report, the stored process will scan the SAS log for any problematic messages (errors, warnings, or bad notes), and if there are bad messages, will return those to the user, instead of the report.  So it's roughly:

1. Run a report and write the report to a file (html/pdf/rtf/SASREPORT), but do NOT display the results to the user.

2. Scan the log from running the report.

3. If the log is clean, I will want to stream the report to user.  If the log has errors, I will display an error message to the user.

[Another option I played with was using ods document as a tool to hold the report and later replay it.]

I'm new to working on stored processes/application development for use by non-programmers.  As a SAS programmer, I always run a %logcheck on my log before trusting (even looking at) results. So I feel like as a developer, I should give users the same level of error-detection.  Of course ideally code will trap errors before they are encountered.  But especially in the BI world, where the person who programs a stored procedure may not have any control over the live source databases that are inputs to the procedure, it seems worth checking the log, rather than assuming that if SAS returned a result, everything must have run fine.

But I'd be happy to hear thoughts from you or others on this sort of approach.  I've tried to start threads here and on SAS-L, with little interest generated.  So maybe I'm off-base.  But in my gut, I feel like it would be close to negligence to release an application that used SAS but completely ignored the SAS log....

--Q.

Contributor
Posts: 45

Re: How to display a pdf/html/rtf file?

I didn't mean to imply that your concern is not justified. I have only worked in one SAS shop, so I am curious how other places work.

However, if you don't have control over the data that is being used for your BI reports...then I think that is a big problem.

PROC Star
Posts: 1,233

Re: How to display a pdf/html/rtf file?

I share your curiousity (I've worked in several SAS shops, but this is my first time working in BI).

I guess I meant I personally do not have control overt the databases that my stored processes consume.  The databases themselves are maintained by the DBAs.

So I could "trust" the databases (that all the integrity constraints are defined, etc etc).  And then if bad data snuck in and somebody noticed  my stored process returned an incorrect report, I would claim "Garbage in, Garbage out, blame the DBA."

But over the years, I've learned NOT to trust that data are clean (even when I do manage the database : )   And if a report generated by my stored procedure is incorrect, I don't thin folks will be happy with finger-pointing. So if the SAS log can tell me that missing values were created for some records because values were missing/invalid  (or a 100 other messages), I don't want to return a misleading report to the user.

Solution
‎05-24-2012 02:20 PM
PROC Star
Posts: 1,233

Re: How to display a pdf/html/rtf file?

I think I've got streaming of a file back to _webout working.  Instead of using %filesrv for the pdf, I found a tip on serving a binary file: http://support.sas.com/kb/6/588.html  .

Below is my current code for streaming a file sitting on the server (html, rtf , pdf):

data _null_;
  file _webout;
  infile myhtml;
  input ;
  put _infile_;
run;

data _null_;
  file _webout;

  infile myrtf;

  if _n_ = 1 then do;
    rc = stpsrv_header('Content-type','application/msword');

    rc = stpsrv_header('Content-disposition',"attachment; filename=SampReplay_rtf.doc");

  end;
  input ;
  put _infile_;
run;

data _null_;
  file _webout recfm=s;
  infile  mypdf recfm=n;
  if _n_ = 1 then do;
    rc = stpsrv_header('Content-type','application/pdf');
    rc = stpsrv_header('Content-disposition',"attachment; filename=SampReplay_pdf.pdf");
  end;
  input c $char1.;
  put c $char1. @@;
run;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 1232 views
  • 0 likes
  • 2 in conversation