Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set dpi using phantomjs node?

I am using phantomjs node module. below is the link where from i get this module.

https://github.com/sgentle/phantomjs-node

this really work nice when i create a jpg image file from html. but its default "dpi" is 72 which is not good for printing the image file. so i want to set the dpi when i rendering the image.

from the above link i read some example like page.set('viewportSize', {width:640,height:480}). its set the image size perfectly. but i want to set dpi of image please guide me how can i achieve this.

var phantom = require('phantom');

phantom.create(function (ph) {
  ph.createPage(function (page) {
    page.open("http://www.google.com", function (status) {
       page.render("bla.jpg");
        ph.exit();

    });
  });
});  
like image 781
Bharat Bhushan Avatar asked Aug 06 '14 07:08

Bharat Bhushan


1 Answers

There are two ways.

1. Zoom

You can use page.zoomFactor to zoom the page. You have to increase the viewport size accordingly before changing the zoomFactor:

page.viewportSize = { width: 1600, height: 800 };
page.zoomFactor = 300.0/72.0;
page.render("zoom4.jpg");

2. PDF

If you are concerned with the quality, then render a pdf of the page. A pdf is vector-based and you can zoom in as much as you like. It works by using the pdf extension when you give the filename to render:

page.render("bla.pdf");

You can see how the rasterize.js example deals with the page sizes. There are some caveats though. You have to keep in mind to adjust the width of the rendered pdf. See for example this (unanswered) question.

like image 141
Artjom B. Avatar answered Nov 08 '22 03:11

Artjom B.