Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
129
rated 0 times [  133] [ 4]  / answers: 1 / hits: 24915  / 6 Years ago, mon, may 28, 2018, 12:00:00

I'm calling a web service to generate a .pdf, then using createObjectURL and and iframe to print and display it:



    var title = Claim- + this.claimNumber + - + new Date() + .pdf;
var blob = new Blob([wsRequest.response], { type: 'application/pdf' });
blob.name = title;
if (browser() === 'IE') {
window.navigator.msSaveOrOpenBlob(blob, title);
} else {
var fileURL = URL.createObjectURL(blob);
var win = window.open();
win.document.write('<iframe name=' + title + ' src=' + fileURL + ' frameborder=0 style=border:0; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%; allowfullscreen></iframe>');
win.document.title = title;


For IE, it works great: the .pdf comes up in Acrobat Reader, it displays, I can print it ... and it has a meaningful filename.



For Chrome/embedded .pdf viewer, it also works OK: it comes up in it's own tab, and the tab has a meaningful filename.



If Chrome brings up the image in Acrobat reader, however:



a) I get a new, blank tab (with the meaningful name)



b) Acrobat displays a GUID - the GUID assigned by createObjectURL():



EXAMPLE: blob:http://192.168.116.170:9080/dd554e89-0174-4b9a-bbd1-0934239a4c9



As you can see, neither blob.name = title or <iframe name= + title + ...> seem to help.



Q: Is there any way I can assign a meaningful name to a dynamically generated .pdf if Chrome opens it in an external viewer (like Acrobat)?


More From » html

 Answers
34

One way is to save the file with a filename before it's opened. Unfortunately, this may not automatically open the file.



var fileLink = document.createElement('a');
fileLink.href = fileURL;
fileLink.download = title;
fileLink.click();


Another way is to generate the PDF and filename on your web server, and offer the link remotely, rather than generate the filename locally in the browser. This might offer you more consistent timestamps because they are generated by your server rather than all the clients in different timezones. Then you and your customers will be able to logically refer to identical documents if they have any questions.


[#54327] Thursday, May 24, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
domeniccolti

Total Points: 276
Total Questions: 98
Total Answers: 93

Location: India
Member since Fri, May 13, 2022
2 Years ago
domeniccolti questions
Mon, Oct 18, 21, 00:00, 3 Years ago
Thu, Oct 14, 21, 00:00, 3 Years ago
Thu, Jul 15, 21, 00:00, 3 Years ago
Sat, Oct 24, 20, 00:00, 4 Years ago
Thu, Sep 3, 20, 00:00, 4 Years ago
;