how to show a pdf file with Wt ?
Added by Christophe Delépine over 14 years ago
Hey,
I have a trivial question : when the user clicks on a button, i want to open a pdf file and show its content (in another tab).
How to do this in Wt ?
Thanks
Replies (11)
RE: how to show a pdf file with Wt ? - Added by Christophe Delépine over 14 years ago
I have found a way :
WAnchor* anchor = new WAnchor();
WFileResource* fileResource = new WFileResource(fullpath.c_str());
fileResource->setMimeType("application/pdf");
anchor->setResource(fileResource);
anchor->setTarget( Wt::TargetNewWindow );
anchor->setText(pdfSumName.c_str());
It works, however, the new tab window has a name that corresponds to the deployment path ("hello").
Is it possible to set the name of the new tab window ?
Regards
Christophe
RE: how to show a pdf file with Wt ? - Added by Koen Deforche about 14 years ago
Hey Christophe,
Yes, using WResource::suggestFileName().
(Sorry for the late reply, I hope it is still useful).
Regards,
koen
RE: how to show a pdf file with Wt ? - Added by Christophe Delépine about 14 years ago
Hi Koen,
I have tried your suggestion but the behavior is now completeley changed : instead of showing the pdf in a new tab, the browser prompts for opening/saving the file. If i click on open, Adobe Reader is launched.
Thanks anyway for your answer
Christophe
RE: how to show a pdf file with Wt ? - Added by Koen Deforche about 14 years ago
Christophe,
So although your browser has a plugin for showing PDFs, it isn't using it?
And you are using a filename with a "pdf" extension ?
Regards,
koen
RE: how to show a pdf file with Wt ? - Added by Christophe Delépine about 14 years ago
Yes and yes
If i don't call fileResource->suggestFileName(pdfSumName.c_str()); then a new tab is created and the pdf file is shown inside automatically.
But the tab name does not reflect the pdf filename
If i call fileResource->suggestFileName(pdfSumName.c_str()); then a tab is not created anymore.
It is not a big problem, so don't spend too much time on it, unless you think there is a bug.
Regards
Christophe
RE: how to show a pdf file with Wt ? - Added by Koen Deforche about 14 years ago
Wim,
Could this be because of some of the headers you added which now force the browser to show the "Save as" dialog rather than seeing whether it has a plugin to hande it?
koen
RE: how to show a pdf file with Wt ? - Added by Wim Dumon about 14 years ago
Christophe,
Koen,
I only modified the way the filename is encoded to support unicode characters in the filename in most browsers. Content-Disposition has always been set to 'attachment', which suggests the browser to display a 'save as' dialog. The alternative is to set this to 'inline', which indicates preference to use a plug-in to display the contents.
Christophe, could you test to see if you have the desired behavior when you change src/Wt/WResource.C, around line 100, to this:
response.addHeader("Content-Disposition",
"attachment;filename=" + suggestedFileName_);
If so, we'll add a method to WResource to indicate your preference to open the resource inline or with a save as dialog.
BR,
Wim.
RE: how to show a pdf file with Wt ? - Added by Christophe Delépine about 14 years ago
Hi Wim,
I have tried your suggestion but the behavior hasn't changed.
Regards
RE: how to show a pdf file with Wt ? - Added by Wim Dumon about 14 years ago
Hi Christophe,
I assume you spotted my silly mistake, the code example should have read:
response.addHeader("Content-Disposition",
"inline;filename=" + suggestedFileName_);
With the modification above applied, I quickly tested this myself:
WAnchor* anchor = new WAnchor(root());
WFileResource* fileResource = new WFileResource("c:/tmp/Wt-embedded.pdf");
fileResource->suggestFileName("wt-embedded.pdf");
fileResource->setMimeType("application/pdf");
anchor->setResource(fileResource);
anchor->setTarget( Wt::TargetNewWindow );
anchor->setText("click here to view the pdf");
When not deployed on '/', but e.g. on '/hello' (---deploy-path=/hello), the URL looks like this:
http://localhost:8080/hello/wt-embedded.pdf?wtd=bRQGmZgrNCJ1Pu5V&request=resource&resource=o5cn&rand=7
and the titlebar in firefox is 'wt-embedded.pdf'. In Chrome and IE8, the title bar is the entire URL.
The URL is not nice-looking when the application is deployed on '/'.
BR,
Wim.
RE: how to show a pdf file with Wt ? - Added by Christophe Delépine about 14 years ago
Hi Wim,
No i hadn't noticed, i can be very silly too.
That explains why i had the same beahvior..
now it works as you describe
I guess you will add a method to specify inline or save as.
Thanks
Christophe
RE: how to show a pdf file with Wt ? - Added by Wim Dumon about 14 years ago
Yes, I created an feature request (#778)