Last active
February 4, 2019 17:04
-
-
Save tresf/f9026c8c18ab96e230015053fb33778d to your computer and use it in GitHub Desktop.
QZ Tray 2.1 Page Break
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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