12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- var page = require('webpage').create();
- var format = '#{format}';
- var data = '#{data}';
- var scale = parseFloat('#{scale}', 10);
- var width = parseInt('#{width}', 10);
- var height = parseInt('#{height}', 10);
- try {
- var pdfOptions = JSON.parse('#{pdf}' || null);
- } catch (e) {
- console.error("Parsing PDF options failed.");
- }
- try {
- var jpegOptions = JSON.parse('#{jpeg}' || null);
- } catch (e) {
- console.error("Parsing JPEG options failed.");
- }
- if (format === 'pdf') {
- page.paperSize = pdfOptions || { format: 'Letter', orientation: 'portrait', border: '1cm' };
- }
- page.content = '<img id="chart" src="' + data + '" style="-webkit-transform-origin: left top">';
- // Let the content to render, then adjust viewport, clipRect and image size.
- window.setTimeout(adjustSize, 1);
- function adjustSize() {
- var naturalSize = page.evaluate(function () {
- // By default the body has some margin, but we don't want that to be a part of the rendered image.
- var style = document.createElement('style'),
- text = document.createTextNode('body { margin: 0 }');
- style.setAttribute('type', 'text/css');
- style.appendChild(text);
- document.head.insertBefore(style, document.head.firstChild);
- var img = document.getElementById('chart');
- return {
- width: img.naturalWidth,
- height: img.naturalHeight
- };
- });
- if (width && height) {
- if (width !== naturalSize.width || height !== naturalSize.height) {
- page.viewportSize = { width: width, height: height };
- page.clipRect = { left: 0, top: 0, width: width, height: height };
- }
- }
- width = Math.round((width || naturalSize.width) * scale);
- height = Math.round((height || naturalSize.height) * scale);
- page.evaluate(function (width, height) {
- var img = document.getElementById('chart');
- img.style.width = width;
- img.style.height = height;
- }, width, height);
- // Let the image to scale before rendering.
- window.setTimeout(render, 1);
- }
- function render() {
- page.render('#{filename}', { format: format, quality: jpegOptions && jpegOptions.quality || 80 });
- phantom.exit();
- }
|