I need to show the preview of an invoice. For that I have to scale the size of preview according the div to show the preview. This div is responsive like if we reduce the size of window the size of div also get reduced and hence I have to scale the preview according to the div.
Since invoice paper size in mm so i need to get the innerWidth of div in mm so that I can get the proper scale value.
Any other alternative to achieve the same is also welcome. In case of 'px', its working properly, but i need to get the width in mm
$("#preview").css({
border : "1px solid silver",
width : "794px",
height : "1123px",
position : "relative",
margin : "auto",
boxShadow : "2px 2px 4px silver"
});
// Scale preview with screen size
var updatePreviewScale = function(cssObj){
$("#preview").css(cssObj);
};
// A4 paper size in pixel : 794px X 1123px
var scalePreview = function(){
var width = $("#preview-container").innerWidth();
var expectedWidth = 794;
var expectedHeight = 1123;
var ratio = width / expectedWidth;
if(ratio > 1){
return false;
}
var widthMargin = -(expectedWidth * (1 - parseFloat(ratio)))/2;
var heightMargin = -(expectedHeight * (1 - parseFloat(ratio)))/2;
var cssObj = {
"-webkit-transform" : "scale(" + ratio+ ")",
"-moz-transform" : "scale(" + ratio+ ")",
"-ms-transform" : "scale(" + ratio+ ")",
"transform" : "scale(" + ratio+ ")",
"left" : widthMargin,
"top" : heightMargin
};
updatePreviewScale(cssObj);
};
scalePreview();