11

Using phantomjs, is there a way to control the DPI setting used when rasterizing an image of the web content using the page.render(filename) method?

I can't find anything that would control this via the interface api, but didn't know if someone has already figured out a way to do this.

Our business-case looks like this:

Custom HTML content created via a web application is fed to our rasterize.js phantom process and is queried for a specific tag to set the client rectangle. This client rectangle is rendered to a PNG of the HTML that can then be used as an image elsewhere.

We want the resolution of the resulting PNG to be something higher than the default, due to aliasing on the text at some odd font sizes/bold combinations.

Thomas Jones
  • 4,892
  • 26
  • 34
  • The resolution recorded in a PNG is generally ignored when displaying it. Does it look OK when you open it in an editor? – Mark Ransom Jun 07 '12 at 16:58
  • 2
    no, and that's the issue. They are including the PNG inside a PDF, which is supposedly maintaining resolution, and it's producing "hard-to-read text" – Thomas Jones Jun 07 '12 at 17:14
  • Ah, you've found the one situation where resolution isn't ignored. You probably want a resolution of 96. – Mark Ransom Jun 07 '12 at 17:15
  • yes, but is it possible within phantom JS to specify what resolution at which to `render()` it? FWIW, they are requesting 300 DPI, since its meant to be a printed item. – Thomas Jones Jun 07 '12 at 17:20

2 Answers2

2

I may be mixing up DPI with something else, but have you looked into the zoomFactor option? Setting that on the page object will cause the rendered image to zoomed.

Henrik Aasted Sørensen
  • 6,966
  • 11
  • 51
  • 60
0

This fork allows setting the dpi https://github.com/martonw/phantomjs/tree/issue-%2313553

You can set then the dpi with page.dpi = 72

console.log('Loading a web page');  
var page = require('webpage').create();
var url = 'http://phantomjs.org/';
page.open(url, function (status) {
  //Page is loaded!
  page.dpi=300; // this is where you actually set the DPI
  page.render("test.pdf");
  phantom.exit();
});
mapodev
  • 988
  • 8
  • 14