So, I have dynamically created a pdf, and now i want to print it:
var doc = new jsPDF();
var name = "Doe, John"
doc.setFontType("normal");
doc.setFontSize(12);
doc.text(20,20,'Name: '+ name);
//do something that prints the pdf...
So, how do I take this doc variable and print it. Everywhere else I found uses the url of the pdf. Do I need to create a url for it first?
So, the solution I am currently going with is to show the pdf in a new tab/window, from which the pdf can be printed.
window.open(doc.output('datauristring'));
Unfortunately, this only works in Chrome. Anyone know how to get it working in IE, Firefox, Safari, etc.?
I still wonder if there is a way to skip this step (of opening the pdf and then requiring another button to be pushed).
There is a method autoPrint() provided by jsPDF library.
You can use that as shown below
var doc = new jsPDF();
var name = "Doe, John"
doc.setFontType("normal");
doc.setFontSize(12);
doc.text(20,20,'Name: '+ name);
doc.autoPrint();
//This is a key for printing
doc.output('dataurlnewwindow');
Try this:
window.open(doc.output('bloburl'), '_blank');
This can have problems with adblock sometimes.
So, in conclusion, for Chrome and Safari, use
window.open(doc.output('datauristring'));
but for IE and Firefox, use
doc.save();
These will both allow you to open the pdf in a new window, from which it can be printed. For those who have taken the time to figure out what is necessary on other browsers, feel free to add your research here...
All you need is to add this
doc.save('Test.pdf');
(I suppose you already have all the above written code inside the button trigger which user clicks to get pdf)
You better implement "printjs" from npm, convert jspdf document to blob and print it like this
const data = PDF.output('blob')
const blobUrl = URL.createObjectURL(data);
printJS(blobUrl);
Related
I have received a PDF from a server in which the user often wants to print without really looking at it. I try to use an onafterprint event to close the window, however, it foes not seem to get triggered when I open a PDF this way. If I instead set the URL blank to open an empty page it seems to work just fine.
let url = URL.createObjectURL(blob);
let printWindow = window.open(url);
printWindow.onafterprint = function(){
console.log("afterprint")
};
Your code won't get executed since the pdf window MIME type is "application/pdf", not "text/html".
Check this one for more details.
https://stackoverflow.com/a/51735450/1141936
I have a simple javascript application that lets the user generate some XML and then save it as a file. However I can't get the saving to work in IE.
Currently I am using the data-uri method as exemplified in this jsfiddle. But this does not work in IE because it does not support data-uri for application/xml. What would be another method or workaround (client-only) to let the user easily save an xml string (or dom node) as a file?
The correct answer was given by Mr Anonymous in the comment.
$("a").click(function(e){
var xml = $("textarea").text();
if(window.navigator && window.navigator.msSaveBlob){
e.preventDefault();
navigator.msSaveBlob( new Blob([xml], {type:'application/xml'}), "myfile.xml" )
} else {
$(this).attr("href", "data:application/xml," + encodeURIComponent(xml));
}
});
how can i append data to a file using javascript?
i tried to use this code, but i got an error:
var fso = new ActiveXObject("Scripting.FileSystemOject");
var filepath = fso.GetFile("member.txt");
var fileObject = fso.OpenTextFile(filepath, 8);
file.WriteLine(id + "|" + pass);
fileObject.close();
the error is on var fso = new ActiveXObject("Scripting.FileSystemOject");, written: Error: Automation server can't create object
is there any other way to append the file using javascript or the way to fix this? thanks :)
EDIT:
i have doing what's written on this, and it still not working :/
I just realized these in your code:
var fileObject = fso.OpenTextFile(filepath, 8,true);
You'll need the true-argument, if the file does not exist, or you want to overwrite/append it.
var filepath = fso.GetFile("member.txt");// This won't work.
var filepath = "your_filePath"; // Use this instead
var fileObject = fso.OpenTextFile(filepath, 8, true);
OpenTextFile() needs a path as a string like "D:/test/file.txt". GetFile() returns an object, which you can see as a string (D:\test\file.txt), but it's not a string. Use also absolute paths, relative paths don't seem to work by my experience.
EDIT
Add the code below to the <head>-part of your html-file, then save locally as a hta (with file extension hta, not htm or html).
<hta:application
applicationName="MyApp"
id="myapp"
singleInstance="yes"
/>
Then run the hta-file. If you still getting an ActiveX-error, it's not supported by your OS. If this works, you haven't done all the security settings correct.
EDIT II
In this case it's not very usefull to get the path through ActiveX, you'll need to write it literal anyway. And I'm not supposed to do your homeworks, but this does the trick...
var filepath = new String(fso.GetFile("member.txt")).replace(/\\/g,'/');
And don't forget what I've said above about using absolute paths...
The 8 in the OpenTextFile function specify that you want to append to the file. Your problem comes from the security restriction of your browser. To make it work you'll have to lower the security level, which is not really recommended.
The error is thrown because there are security restrictions which donot allow the activex to run. change your security settings to allow the activex if your using internet explorer (which i think you are).
This might be useful http://windows.microsoft.com/en-US/windows/help/genuine/ie-activex
Cheers
EDIT: i have doing what's written on this, and it still not working :/
* try Restarting your browser
As pointed out in this comment
Javascript: how to append data to a file
the cause of the error Error: Automation server can't create object is the typo in the progid passed to ActiveXObject: Oject instead of Object:
var fso = new ActiveXObject("Scripting.FileSystemOject");
there is a missing b!
I would like to generate a text file in the javascript dynamicly, then offer this for download. Currently I can get this working to a degree with either of the following solutions:
content = "abc123";
document.location = "data:text/octet-stream," + encodeURIComponent(content);
OR
content = "abc123";
var bb = new BlobBuilder();
bb.append(content);
var blob = bb.getBlob();
blob = blob.slice(0, blob.size, 'text/octet-stream');
var fr = new FileReader();
fr.onload = function() {document.location = this.result;}
fr.readAsDataURL(blob);
However, Both of these solutions, when the download box appears, will only offer a default filename of 'download' in the save as dialogue.
My question is basically, how can I change this to a specific filename for example 'readme.txt' or 'scene.obj'
Also note the data type was previously 'text/plain' however if this is used, the document switches to the new text document instead of offering it for download (as text/octet-stream seems to do).
I do not want a flash solution, javascript/html5 only suggestions please.
Cheers, Josh
For that, you will have to use FileSaver from FileAPI: Writer specification.
For now, it's only a draft, and according to mailing list answer it isn't yet implemented in browsers.
You can watch for example on a chromium issue to get up-to-date information about the implementation progress
UPD 02.08.2013: I have since found a project that provides FileSaver interface using neat tricks
I think you should check: jQuery Table to CSV export
I'm using Silverlight.CreateObject to create a silverlight object dynamically. when silverilght is not installed it shows its own patent image from microsoft. I want to replace this image with mine...how can I do this??
Determine if Silverlight is installed, and then do whatever you want based on the result...
(you can find the contents of isSilverLightInstalled() at the linked site above)
function isSilverLightIntstalled() {
// see website
}
if (!isSilverLightInstalled()) {
var myImage = new Image;
myImage.src = “image URL goes here”;
document.appendChild(myImage);
}