In my previous blog, How to customize data export in SAS Visual Analytics?, I explained how we can export customized data from SAS Visual Analytics. The generated export file was directly downloaded after execution of the SAS Viya Job used to generate the export file. I received questions from colleagues about generating a PDF or displaying a PDF into SAS Visual Analytics. I will focus on the integration in SAS Visual Analytics.
If you have a list of existing PDF files, you can upload them manually using SAS Drive or programmatically using REST APIs. Here is the documentation for the point-and-click manual load or to upload files using REST APIs.
If you don't have the files, creating a PDF file can be done using SAS code with a syntax like this one:
ods pdf ;
proc print data=sashelp.class;
run;
ods pdf close;
Storing the generated file in SAS Content Server can be done using the filesrvc filename statement. For example, the following code will create a PDF file into the PDF folder located under Public folder:
filename myPDF filesrvc folderpath='/Public/PDF' name="class.pdf" contenttype="application/pdf";
You can then adapt the ods pdf statement to write the file into that location:
ods pdf file=myPDF;
It means that you can generate the PDF files dynamically and pass the URI as described in the article, How to customizee data export in SAS Visual Analytics?.
More information about the filesrvc filename can be found here.
For this article, we will not use that technique as we will use existing files and store the links to the files in a table loaded into CAS.
To retrieve the file URI, you can use SAS Drive:
Select any image to see a larger version.
Mobile users: To view the images, select the "Full" version at the bottom of the page.
You can then store that URI in a table and load it into CAS. For this demo, only two files will be used, and the references will be added as variable in a copy of the sashelp.class table which is loaded into CAS using the following code:
cas;
libname casuser cas caslib=casuser;
data casuser.pdflinks;
set sashelp.class;
if sex="F" then
URI="/files/files/e418fab4-bb7c-46c0-87ff-bbb4ae1e3a2f";
else
URI="/files/files/f7159d8a-ec09-48cb-95d1-4c5c1fbd4de3";
run;
proc casutil;
promote casdata="PDFLINKS";
run;
Note: promoting the table is necessary to make it persistent between CAS sessions. In this case, the report can't be shared with others as the data will be stored in the CASUSER CAS library. If you want to share it with others, you should choose a CAS library which is available to group users who will access the report.
At this point, you have the needed data to display a PDF in a report. It is now time to create a report which will allow the user to select an observation in a list table and display the PDF below.
To display the PDF dynamically, we must pass the URI information to the object at the bottom. This object is a Data-Driven Content object. It means that we will have to create an HTML page to display the PDF. This page will be described in the next section.
To create the link between the List Table object and the Data-Driven Content object, you should:
For this example, our Data-Driven Content object only needs the URI variable, but you can configure it to pass additional data columns if you need.
As mentioned earlier, we need to build an HTML page which will be responsible for the rendering of the PDF. The HTML page will receive data from the SAS Visual Analytics report based on the selection done in the List Table object. Using the data, it will send a request to the SAS Viya server to retrieve the file based on the URI we collected from SAS Drive.
While this may seem a bit complex, the HTML page has only a few lines:
On line 18, we define an iFrame which will embed the PDF file.
Between line 34 and 39, we defined an event listener which will detect that the selection in the List Table has changed and call the displayPDF function passing the data received from SAS Visual Analytics.
The object received from SAS Visual Analytics has the following structure:
In the columns array, you can find the variable information. As you can see, the Data-Driven Content object received the variables in the order we defined in the Roles of the object. Knowing that, we can retrieve the URI from the first observation in the data array. In that observation/array, the URI has an index of 1. For that reason, on line 22 of the code, we can retrieve the fileURI using the following instruction:
const fileURI = event.data.data[0][1]
Using the fileURI, we can build the URL (on line 24) from which the PDF file can be retrieved (on line 25).
As soon as the PDF file is downloaded from the SAS Content Server using the SAS REST APIs, the file is converted into a blob representation (line 26) and that blob is then associated as src for the iframe (lines 27 to 29). More information about downloading a file from the SAS Content Server using REST APIs can be found here.
In my previous article, How to customize data export in SAS Visual Analytics?, I explained how to export customized data from SAS Visual Analytics. In this article, I explained how you can display a PDF inside your SAS Visual Analytics. If you want to combine the articles to generate a PDF file dynamically using SAS Viya Jobs and store that file in the SAS Content Server and view the result directly in the SAS Visual Analytics report, you can merge the concepts covered in the two articles. This should give you the opportunity to enhance user experience.
If you want to deploy the application on your cluster, you can use the manifest file provided to you under deployment_assets in the following git repository. The code for the HTML and the sample PDF files are also available in the same repository.
Important note: due to extra security implemented in Chromium based browsers (Google Chrome, Microsoft Edge, Brave, ...), a message like this "This page has been blocked by Microsoft Edge" might be displayed. Testing in Mozilla Firefox gives the correct result.
Find more articles from SAS Global Enablement and Learning here.
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!
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.