when we use addHTML(), generated pdf quality is poor.
To improve quality, I write this code.
var pdf = new jsPDF('l', 'in', 'a4');
pdf.internal.scaleFactor = 30;
pdf.addHTML($('#print-area')[0], function () {
pdf.save(calendarName);
});
This may help someone
when we use addHTML(), generated pdf quality is poor.
To improve quality, I write this code.
var pdf = new jsPDF('l', 'in', 'a4');
pdf.internal.scaleFactor = 30;
pdf.addHTML($('#print-area')[0], function () {
pdf.save(calendarName);
});
This may help someone
Related
I´m trying to generate a PDF on my application, using something like this, from documentation:
var doc = new jsPDF();
doc.html(document.body, {
callback: function (doc) {
doc.save();
}
});
But what I need is to get this generated file, as a base64 content, to send as an attachment on an email. Is there a way to get this directly on callback?
It's worth noting that the datauri option changes the document location, see the following snippet from jsPDF lib:
case 'datauri':
case 'dataurl':
return global.document.location.href = datauri;
This is fine unless you are trying to use an IFrame, as it would cause its body to be replaced by an embed tag displaying the pdf just generated.
Instead, the safest option is to use datauristring as this simply returns the pdf in a base64 string:
var pdf = new jsPDF('p', 'pt', 'a4');
var base = pdf.output('datauristring'); // base64 string
console.log("base64 is ", base);
Honestly, I don't know why anybody would want to use the datauri option instead of datauristring as latter's behaviour it's what most people expect anyway.
You can do like below.
var pdf = new jsPDF('p', 'pt', 'a4');
pdf.html(document.getElementById('doc'), {
callback: function (pdf) {
// example text
pdf.text(20, 20, 'Hello world!');
pdf.text(20, 30, 'This is client-side Javascript, pumping out a PDF.');
var base = pdf.output('datauri'); // directly to base664
console.log("base64 is ");
console.log(base);
// you can generate in another format also like blob
var out = pdf.output('blob');
var reader = new FileReader();
reader.readAsDataURL(out);
reader.onloadend = function() { // for blob to base64
base64data = reader.result;
console.log("base64 data is ");
console.log(base64data );
}
pdf.save('DOC.pdf');
}
})
You can see more on output() method in the following link.
jspdf output() source code
Below code will generate a pdf which contains only text and does not include any images or styles.
var doc = new jsPDF();
var source = document.getElementById("dividofhtml");
doc.fromHTML(source,15,15);
doc.save();
The below code will include images and css styling in pdf.
html2canvas($("#up"), {
onrendered: function(canvas) {
var imgData = canvas.toDataURL(
'image/png');
var doc = new jsPDF('p', 'mm');
doc.addImage(imgData, 'PNG', 0, 0);
doc.save('text.pdf');
}
});
I am unable to use imagemagick in meteorjs. I am working on a small svg->png converter which contains a rest api to provide the converted images. I implemented the rest api with meteor-router. The imagemagick convertion works. But, I am not able to write the result of the convertion into the http response. I tried to fix this by getting rid of the asynchronisity by using fiber. But this still doesn't work. Basically, all request.write calls are ignored after the yield execution. Here is my code:
Meteor.Router.add({
'/image/:hash' : function(hash) {
var svg = Images.findOne({'hash' : hash}).svg;
var request = this.request;
var response = this.response;
Fiber(function() {
var fiber = Fiber.current;
response.writeHead(200, {'Content-Type':'image/png'});
var convert = imagemagick.convert(['svg:-', 'png:-']);
convert.on('data', function(data) {
response.write("doesn't work");
//response.write(data);
});
convert.on('end', function() {
response.write("doesn't work");
//response.end();
fiber.run({});
});
convert.stdin.write(svg);
convert.stdin.end();
response.write("works");
Fiber.yield();
response.write("doesn't work");
}).run();
}
});
I am pretty new to meteorjs. Therefore, I might use Fiber completely wrong. Or I should not use fiber at all. Can someone help?
Thanks to the author from meteor-router, I was able to fix the problem. I was using fiber the wrong way. As described at https://github.com/laverdet/node-fibers#futures, it's not recommended to use fiber without an abstraction between your code and the raw API.
Fortunately, fiber provides one abstraction called future which can be used for my use case! Here is the working code:
var require = __meteor_bootstrap__.require,
Future = require('fibers/future');
Meteor.startup(function() {
Meteor.Router.add('/image/:hash', function(hash) {
var response = this.response;
var fut = new Future();
response.writeHead(200, {'Content-Type':'text/plain'});
setTimeout(function(){
response.write("hello hello");
fut.ret();
}, 1);
fut.wait();
});
});
I did some more investigation. The issue is orthogonal to imagemagick. E.g.: the following code snippets do not work too in meteor-router:
Example 1:
Meteor.startup(function() {
Meteor.Router.add({
'/image/:hash' : function(hash)
var request = this.request;
var response = this.response;
response.write("outside");
setTimeout(function(){
response.write("inside");
response.end();
}, 1);
}
});
Example 2:
Meteor.startup(function() {
Meteor.Router.add({
'/image/:hash' : function(hash)
var request = this.request;
var response = this.response;
response.write("outside");
Fiber(function() {
var fiber = Fiber.current;
setTimeout(function(){
response.write("inside");
response.end();
}, 1);
Fiber.yield();
}).run();
}
});
I think it's general issue of meteor-router. Because both examples do work with pure nodejs.
I've been struggling to instantiate Blobs in dartlang as of late, but i worked out how to do it in version 8640
For those interested (I couldn't find an example anywhere on the net)
I did this:
HttpRequest req = new HttpRequest();
FileReader fileReader = new FileReader();
req.open("GET", "http://...", true);
req.responseType="blob";
req.overrideMimeType("image/png");
req.on.load.add( (event) {
if(req.readyState==4) {
Blob blob = req.response; // note NOT req.responseBlob
fileReader.on.load.add( (evt) {
document.query('#myimage').src=evt.target.result;
});
fileReader.readAsDataURL(blob);
}
});
req.send();
I hope this helps somebody :)
If anyone landed here trying to figure out how to call the Blob constructor like I did, you actually need to feed it a list of lists (i.e. a List<List<int>>).
So, if you have a list of integers:
var binary = [1, 2, 3];
You pass it to the Blob constructor thusly:
var blorp = new Blob([binary]);
Calling var blorp = new Blob(binary); will result in a confusing message, claiming that the constructor wants a String.
From the answer embedded in the question above:
HttpRequest req = new HttpRequest();
FileReader fileReader = new FileReader();
req.open("GET", "http://...", true);
req.responseType="blob";
req.overrideMimeType("image/png");
req.on.load.add( (event) {
if(req.readyState==4) {
Blob blob = req.response; // note NOT req.responseBlob
fileReader.on.load.add( (evt) {
document.query('#myimage').src=evt.target.result;
});
fileReader.readAsDataURL(blob);
}
});
req.send();
I have been using the following code to grab a photo and display it in html works great.
function takePicture() {
navigator.camera.getPicture(
function(uri) {
var img = document.getElementById('camera_image1');
img.style.visibility = "visible";
img.style.display = "block";
img.src = uri;
document.getElementById('camera_status').innerHTML = "Success";
},
{ quality: 50, allowEdit: true, destinationType: navigator.camera.DestinationType.FILE_URI});
};
html later
<img style="width:144px;height:144px;" id="camera_image1" src="nophoto.jpg"/>
However I would like to save the image to the users Library at the same time, any pointer much appreciated.
I have tried using captureImage but this gives me less options like editing and did not place image inline in html.
Thanks again
PhoneGap 1.3
With Phonegap 2.2 you can save the image to the local device.
add "saveToPhotoAlbum : true" to the cameraOptions
function takePicture() {
navigator.camera.getPicture(
function(uri) {
var img = document.getElementById('camera_image1');
img.style.visibility = "visible";
img.style.display = "block";
img.src = uri;
document.getElementById('camera_status').innerHTML = "Success";
}, {
quality: 50,
allowEdit: true,
destinationType: navigator.camera.DestinationType.FILE_URI,
saveToPhotoAlbum : true
});
};
you'll have to change the phonegap code a bit. it wont save the image in the implementation thats there currently.
tell me if you are working on phonegap android. may be able to help u with that.