Skip to content

Commit

Permalink
Make scale-to-page adjust for changes from web dpi
Browse files Browse the repository at this point in the history
  • Loading branch information
Berenz committed Jun 15, 2019
1 parent 8024139 commit 3e64d3f
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions src/qz/printer/action/WebApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class WebApp extends Application {
private static final int SLEEP = 250;
private static final int TIMEOUT = 60; //total paused seconds before failing

private static final double WEB_SCALE = 72d / 96d;

private static WebApp instance = null;

private static Stage stage;
Expand Down Expand Up @@ -75,7 +77,7 @@ public class WebApp extends Application {
base.getAttributes().setNamedItem(applied);
}

//we have to resize the width first, for responsive html, then calculate the best fit height
//width was resized earlier (for responsive html), then calculate the best fit height
if (pageHeight <= 0) {
String heightText = webView.getEngine().executeScript("Math.max(document.body.offsetHeight, document.body.scrollHeight)").toString();
pageHeight = Double.parseDouble(heightText);
Expand All @@ -87,16 +89,8 @@ public class WebApp extends Application {
webView.autosize();
}

//account for difference in print vs web dpi
double scale = 72d / 96d;
webView.getTransforms().add(new Scale(scale, scale));

double increase = 96d / 72d;
log.trace("Setting HTML page width to {}", webView.getWidth() * increase);
webView.setMinWidth(webView.getWidth() * increase);
webView.setPrefWidth(webView.getWidth() * increase);
webView.setMaxWidth(webView.getWidth() * increase);
webView.autosize();
//scale web dimensions down to print dpi
webView.getTransforms().add(new Scale(WEB_SCALE, WEB_SCALE));

snap.playFromStart();
}
Expand Down Expand Up @@ -159,10 +153,10 @@ public static synchronized void print(final PrinterJob job, final WebAppModel mo
PageLayout layout = job.getJobSettings().getPageLayout();
if (model.isScaled()) {
double scale;
if ((webView.getWidth() / webView.getHeight()) >= (layout.getPrintableWidth() / layout.getPrintableHeight())) {
scale = layout.getPrintableWidth() / webView.getWidth();
if ((webView.getWidth() / webView.getHeight()) / WEB_SCALE >= (layout.getPrintableWidth() / layout.getPrintableHeight())) {
scale = (layout.getPrintableWidth() / webView.getWidth()) / WEB_SCALE;
} else {
scale = layout.getPrintableHeight() / webView.getHeight();
scale = (layout.getPrintableHeight() / webView.getHeight()) / WEB_SCALE;
}
webView.getTransforms().add(new Scale(scale, scale));
}
Expand Down

0 comments on commit 3e64d3f

Please sign in to comment.