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)?
URL.createObjectURL() The URL.createObjectURL() static method creates a string containing a URL representing the object given in the parameter. The URL lifetime is tied to the document in the window on which it was created. The new object URL represents the specified File object or Blob object.
The Blob object represents a blob, which is a file-like object of immutable, raw data; they can be read as text or binary data, or converted into a ReadableStream so its methods can be used for processing the data. Blobs can represent data that isn't necessarily in a JavaScript-native format.
What is blob url? Why it is used? Blob URL/Object URL is a pseudo protocol to allow Blob and File objects to be used as URL source for things like images, download links for binary data and so forth. For example, you can not hand an Image object raw byte-data as it would not know what to do with it.
The PDF file will be downloaded as BLOB (Binary Data) using XmlHttpRequest AJAX call and then will be sent for download in the Browser using JavaScript. The PDF file are stored in a folder named Files inside the project directory.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With