Skip to content

Instantly share code, notes, and snippets.

@tresf
Last active February 4, 2019 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tresf/f9026c8c18ab96e230015053fb33778d to your computer and use it in GitHub Desktop.
Save tresf/f9026c8c18ab96e230015053fb33778d to your computer and use it in GitHub Desktop.
QZ Tray 2.1 Page Break
/*
* Page Size Manipulator
*
* Author:
* 2019-02-04 tres@qz.io
* License:
* Public Domain (no restrictions)
* Description:
* Adjusts page style for print medium by reading dimensions from URL
* Created to work around JavaFX inability to read print style information.
* Usage:
* path/to/test.html#4in&206in
* ^--- 4in<space>6in
*
* // QZ Tray API
* var size = { width: '4', height: '6' };
* var units = 'in';
* var config = qz.configs.create('PDFCreator', { units: units, size: size, scaleContent: false });
* var hash = size.width + units + '%20' + size.height + units;
*
* var data = [
* { type: 'pixel', format: 'html', flavor: 'file', data: 'somefile.html#' + hash }
* ];
*
* qz.print(config, data).catch(err => console.error(err));
*/
if (location.hash) {
document.addEventListener("DOMContentLoaded", function(event) {
var cssInject = (function () {
// Append after the last style tag
var existing = document.getElementsByTagName('style');
var style = document.createElement('style');
if (existing.length > 0) {
var sibling = existing[existing.length -1];
sibling.parentNode.insertBefore(style, sibling.nextSibling);
} else {
document.head.appendChild(style);
}
var self = function (rule) {
style.innerHTML = rule;
};
self.size = function(size) {
var dimension = size.split(' ');
var width = "";
var height = "";
var top = "";
var left = "";
switch(dimension.length) {
case 2:
width = 'width: ' + dimension[0] + '; ';
left = 'left: ' + dimension[0] + '; ';
case 1:
height = 'height: ' + dimension[1] + '; ';
top = 'top: ' + dimension[1] + '; ';
break;
case 0:
default:
break;
}
// Attributes to inject, modify as needed
var css = [
'@page {size: ' + size + '}',
'html, body { ' + width + height + '}',
'.flyer, .picking_list, .packing_list { ' + width + height + '}',
'.rotated_wrapper_packing { ' + top + left + '}'
];
cssInject(css.join('\n'));
};
return self;
}());
cssInject.size(decodeURI(location.hash.substring(1)));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment