Opening PDF from web app

Hi, I'm trying to let the user open a help file in PDF format in the Web App version of a tool.
In the Matlab app version I can just do web('UserGuide.pdf') and so long as the file is on the path it works. Or I can find Acrobat on the local machine and use system().
However, when I compile to Web App neither of those options work (and given how small the .ctf file is, it doesn't look like it saves non-Matlab files even if they are called by the app).
Any suggestions much appreciated. I don't really mind if it opens in browser or Acrobat.

 Accepted Answer

web('xxx.pdf') works in compiled Web App, but you need to add the pdf in the "Files required for your app to run" panal by editing project (.prf) file.
In the Web App, web('xxx.pdf') will download the pdf file to the client not viewing in the Web browser. So, I would use uihtml and hyperlink (<a> tag).
After adding HTML component in the design view, adding the following startup function.
function startupFcn(app)
if isdeployed
app.HTML.HTMLSource = fullfile(ctfroot, mfilename, 'test.html');
else
app.HTML.HTMLSource = fullfile(pwd, 'test.html');
end
end
In the test.html file, write the following.
<html>
<a href='some.pdf' target="_blank">Link</a>
</html>
Then, adding pdf and html file in the "Files required for your app to run" panel.

5 Comments

Justin
Justin on 10 May 2023
Edited: Justin on 10 May 2023
Thanks for this, I was creating the Web App from the App Designer Share menu which doesn't offer the "Files required for your app" option. Anyway, using the Web App Compiler the web('UserGuide.pdf') line works as you described, putting the pdf in the Downloads folder when called by the user.
But trying to do the more elegant solution of opening the pdf in the web browser I hit a snag. I was unable to set the uihtml parent to be the uimenu object that calls it, even though the documentation seems to say that the parent can be any uifigure child container.
All right, I understand you want users to view PDF from Menu.
uimenu only allows character vector and string scalar in Text property, so we cannot add hyperlink nor uihtml on it.
Alternatively, we can call menu selected callback and we can create uihtml for viewing PDF.
Here is the sample of menu selected call back.
function PDFdownloadMenuSelected(app, event)
uih = uihtml(app.UIFigure,"Visible", "off");
if isdeployed
uih.HTMLSource = fullfile(ctfroot, mfilename, 'test.html');
else
uih.HTMLSource = fullfile(pwd, 'test.html');
end
end
And test.html should be the following.
<html>
<script>
function downloadPdf(){
document.getElementById('myId').click();
}
</script>
<body onload='downloadPdf()'>
<a href='xxx.pdf' target="_blank" id="myId">Link</a>
</body>
</html>
Don't forget to add test.html and xxx.pdf files in "Files required for your app to run" panel of Web App Compiler.
We can view the PDF by clicking the menu item in the Web App.
OK, so that works in Matlab but as a Web App I get this in the log:
2023-05-10 16:21:57 Error using matlab.ui.control.HTML/set.HTMLSource
2023-05-10 16:21:57 You have specified a file that cannot be found.
Can I ask about the use of mfilename when setting the HTMLSource property. What do I actually want to end up there - the name of the app, the name of the class containing the method that creates the uihtml object, the classname plus method name, or something else?
I think you're getting closer.
If additional files (test.html and xxx.pdf, in this case) are included with "Files required for your app to run" in the Web App Compiler or "mcc -a test.html -a xxx.pdf" command, the files are extracted in the ctfroot folder.
For example, in Windows and MATLAB Runtime is R2022b, pdfApp.ctf would be extracted in
C:\ProgramData\MathWorks\webapps\R2022b\USR\.appCache\mcrCache9.13\pdfApp0\
and this folder can be checked with ctfroot command.
if isdeployed
app.Label.Text = ctfroot;
end
Also, test.html and xxx.pdf files will be located in
C:\ProgramData\MathWorks\webapps\R2022b\USR\.appCache\mcrCache9.13\pdfApp0\pdfApp
and this folder can be checked with fullfile(ctfroot, mfilename) command because mfilename returns pdfApp if executed in pdfApp.ctf.
If the same ctf file will be uploaded, ctfroot will be changed to
C:\ProgramData\MathWorks\webapps\R2022b\USR\.appCache\mcrCache9.13\pdfApp1\
I guess the folder name would be "6 characters + number" (pdfApp0, pdfApp1, ..., pdfApp10,..) but ctfroot command knows the exact location.
That's why I use "app.HTML.HTMLSource = fullfile(ctfroot, mfilename, 'test.html')" command. You should check where test.html and xxx.pdf files are located in the server side and check the location of ctfroot and fullfile(ctfroot, mfilename).
OK, thanks, the issue was mfilename. Without input arguments it returns the classdef file name for the calling method, which is different to the app name. The folder with the PDF in takes the app name. Anyway, have hard coded that bit and it is working.
Now it opens the PDF in a new window so long as the user has their browser as the default for opening PDFs, otherwise it does the download thing.
Thanks again for your help.

Sign in to comment.

More Answers (1)

Praveen Reddy
Praveen Reddy on 10 May 2023
Edited: Praveen Reddy on 10 May 2023
Hi Justin,
I understand that you are trying to open pdf files from MATLAB app using ‘web()’ function and the same is not working when you compile it to Web App. The limitation to ‘web()’ function is that it does not support the text:// URL scheme when opening pages in the system browser or from a deployed application. If you are trying to deploy an application that calls web function using the MATLAB Compiler product, use the ‘-browser’ option to open all pages in the system browser.
Please refer to the following MATLAB documentation to know more about ‘-browser’ option in ‘web()’ function:

1 Comment

Justin
Justin on 10 May 2023
Edited: Justin on 10 May 2023
Thanks, but the '-browser' option doesn't seem to change behaviour in deployed Web App, it still downloads rather than opens the PDF file.

Sign in to comment.

Categories

Products

Release

R2022b

Tags

Asked:

on 3 May 2023

Commented:

on 11 May 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!