BookmarkSubscribeRSS Feed
dipanarya1
Obsidian | Level 7

Hello Team,

 

I am looking for the sample code which can generate and allow user to download the PDF file while using the _webout in data step( data _null_) in stored process.

 

 

Capture.JPGI have tried multiple options using the below content disposition but it is allowing me to download the pdf file but giving an error while opening downloaded PDF file, attached screenshot for your reference.

 

 

 

 

 

 

 

 

%let RV = %sysfunc(appsrv_header(Content-type, application/pdf));
%let RV = %sysfunc(appsrv_header(Content-disposition, %str(attachment; )filename=temp.pdf));

 

 

 

Please note, STP code is working fine and able to generate RTF and Excel.

 

Thanks and Regards,

Dipan Arya

 

 

 

 

13 REPLIES 13
AllanBowe
Barite | Level 11

First of all, try opening your .pdf file in notepad - if there are errors, it will actually contain the log.

 

Secondly, use the `stpsrv()` function methods, as these replace the IntrNet `appsrv` equivalents.

 

Thirdly, don't try and write directly to _webout.  Instead, write to disk and then stream it as binary content (eg with this macro), this way you can at least inspect the file before it is sent (as well as manage multi step PDF writes).

 

Finally, be sure that you haven't used `%stpbegin` or sent any other content to the destination beforehand.

 

Good luck..

/Allan
SAS Challenges - SASensei
MacroCore library for app developers
SAS networking events (BeLux, Germany, UK&I)

Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs
Vince_SAS
Rhodochrosite | Level 12

Can you share the log for the code that is failing?  Something like this should work:

 

%let _ODSDEST=pdf;

%let RV = %sysfunc(appsrv_header(Content-type, application/pdf));
%let RV = %sysfunc(appsrv_header(Content-disposition, %str(attachment; )filename="temp.pdf"));

%STPBEGIN()

proc print data=sashelp.class; run; quit;

%STPEND()

 

Vince DelGobbo

SAS R&D

dipanarya1
Obsidian | Level 7

Hello @Vince_SAS,

 

Thanks for your reply.

 

The pdf is getting generated with HTML tag used in the data step using _webout. i have tried to open it using notepad.

 

Can you please suggest any workaround?

 

Thanks and Regards,

Dipan Arya

Vince_SAS
Rhodochrosite | Level 12

I don't understand what your code is doing.  You need to show the code or the log, or both.

 

Vince DelGobbo

SAS R&D

dipanarya1
Obsidian | Level 7

Hello @Vince_SAS,

 

Thanks for quick reply.

 

Please find attached log for your reference.

 

Thanks and Regards,

 

Dipan Arya

dipanarya1
Obsidian | Level 7

Hello @Vince_SAS,

 

Also attaching the PDF file which is being generated by the code after adding your suggested options to generate PDF.

i have tried to open the file and i can see the HTML TAGs in the PDF file.

 

Thanks and Regards,

Dipan Arya

 

Vince_SAS
Rhodochrosite | Level 12

The "temp.pdf" file in the ZIP archive that you attached is not a PDF file.  It is an HTML file.  Adobe Reader cannot open HTML files.

 

You need to change the logic of your program to create a PDF file when that is the desired type of output.

 

Vince DelGobbo

SAS R&D

dipanarya1
Obsidian | Level 7

Hello @Vince_SAS,

 

Thanks for quick reply.

 

If you check the log file, i applied the _odsdest =pdf and commands to set "appsrv_header" to generate PDF and Initially i also mentioned that the PDF is generated but with the HTML tags. we are using same process to generate the excel and RTF which is working fine but for pdf it is exported with the HTML Tags.

 

Requesting your help.

 

Thanks and Regards,

Dipan Arya

Vince_SAS
Rhodochrosite | Level 12

Yes, I saw that.  I also saw DATA step code that writes HTML to _WEBOUT:

 

 

MPRINT(MAC_REPORT):   data _null_;
MPRINT(MAC_REPORT):   file _webout;
MPRINT(MAC_REPORT):   PUT '<HTML>';
MPRINT(MAC_REPORT):   PUT '<HEAD>';
MPRINT(MAC_REPORT):   PUT '<STYLE>';
MPRINT(MAC_REPORT):   PUT 'TD {';
MPRINT(MAC_REPORT):   put 'font-family:arial;font-size:12px;BORDER-COLOR:BLACK;border-style:SOLID;border-width:1px;text-align: RIGHT;vertical-align: text-bottom; height: 25px';
MPRINT(MAC_REPORT):   put '}';

...

 

 

This is the HTML that I see at the beginning of what you think is a PDF file.

 

 

Microsoft Excel and Microsoft Word can open HTML files, and I suspect that is why it "worked" there.  You did not create true Excel or RTF files.  When you opened the file with Excel and Word you probably got messages indicating that the file type didn't match the file contents.  It is trying to tell you that you don't have the type of file that you think that you have.

 

Vince DelGobbo

SAS R&D

dipanarya1
Obsidian | Level 7
Thanks again @Vince_SAS.

We have to used the HTML tags due to some specific requirements.

Can you please suggest an approach to generate PDF while using the HTML tags in _webout.

Thanks and Regards,
Dipan Arya
Vince_SAS
Rhodochrosite | Level 12

You probably need to get a product that converts HTML to PDF, and then return the resulting PDF file.

 

Vince DelGobbo

SAS R&D

dipanarya1
Obsidian | Level 7
Can you please suggest any product?
Vince_SAS
Rhodochrosite | Level 12

I have never used any.  You will have to search the Web for a product.

 

Vince DelGobbo

SAS R&D

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 13 replies
  • 4478 views
  • 3 likes
  • 3 in conversation