Skip to content

Commit

Permalink
Multiple page support per #157
Browse files Browse the repository at this point in the history
  • Loading branch information
Berenz committed Jun 15, 2019
1 parent ac09a97 commit 8024139
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
31 changes: 31 additions & 0 deletions src/qz/printer/action/PrintImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@ public void parseData(JSONArray printData, PrintOptions options) throws JSONExce
log.debug("Parsed {} images for printing", images.size());
}

private List<BufferedImage> breakupOverPages(BufferedImage img, PageFormat page) {
List<BufferedImage> splits = new ArrayList<>();

Rectangle printBounds = new Rectangle(0, 0, (int)page.getImageableWidth(), (int)page.getImageableHeight());

int columnsNeed = (int)Math.ceil(img.getWidth() / page.getImageableWidth());
int rowsNeed = (int)Math.ceil(img.getHeight() / page.getImageableHeight());
log.trace("Image to be printed across {} pages", columnsNeed * rowsNeed);

for(int row = 0; row < rowsNeed; row++) {
for(int col = 0; col < columnsNeed; col++) {
Rectangle clip = new Rectangle((col * printBounds.width), (row * printBounds.height), printBounds.width, printBounds.height);
if (clip.x + clip.width > img.getWidth()) { clip.width = img.getWidth() - clip.x; }
if (clip.y + clip.height > img.getHeight()) { clip.height = img.getHeight() - clip.y; }

splits.add(img.getSubimage(clip.x, clip.y, clip.width, clip.height));
}
}

return splits;
}

@Override
public void print(PrintOutput output, PrintOptions options) throws PrinterException {
if (images.isEmpty()) {
Expand All @@ -126,6 +148,15 @@ public void print(PrintOutput output, PrintOptions options) throws PrinterExcept
manualReverse = true;
}

if (!scaleImage) {
//breakup large images to print across pages as needed
List<BufferedImage> split = new ArrayList<>();
for(BufferedImage bi : images) {
split.addAll(breakupOverPages(bi, page));
}
images = split;
}

job.setJobName(pxlOpts.getJobName(Constants.IMAGE_PRINT));
job.setPrintable(this, job.validatePage(page));

Expand Down
37 changes: 36 additions & 1 deletion src/qz/printer/action/WebApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.image.WritableImage;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Scale;
import javafx.scene.transform.Transform;
import javafx.scene.transform.Translate;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javafx.util.Duration;
Expand Down Expand Up @@ -89,6 +92,7 @@ public class WebApp extends Application {
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);
Expand Down Expand Up @@ -163,7 +167,38 @@ public static synchronized void print(final PrinterJob job, final WebAppModel mo
webView.getTransforms().add(new Scale(scale, scale));
}

Platform.runLater(() -> complete.set(job.printPage(webView)));
Platform.runLater(() -> {
double useScale = 1;
for(Transform t : webView.getTransforms()) {
if (t instanceof Scale) { useScale *= ((Scale)t).getX(); }
}

PageLayout page = job.getJobSettings().getPageLayout();
Rectangle printBounds = new Rectangle(0, 0, page.getPrintableWidth(), page.getPrintableHeight());
log.debug("Paper area: {},{}:{},{}", (int)page.getLeftMargin(), (int)page.getTopMargin(),
(int)page.getPrintableWidth(), (int)page.getPrintableHeight());

Translate activePage = new Translate();
webView.getTransforms().add(activePage);

int columnsNeed = (int)Math.ceil(webView.getWidth() / printBounds.getWidth() * useScale);
int rowsNeed = (int)Math.ceil(webView.getHeight() / printBounds.getHeight() * useScale);
log.debug("Document will be printed across {} pages", columnsNeed * rowsNeed);

for(int row = 0; row < rowsNeed; row++) {
for(int col = 0; col < columnsNeed; col++) {
activePage.setX((-col * printBounds.getWidth()) / useScale);
activePage.setY((-row * printBounds.getHeight()) / useScale);

job.printPage(webView);
}
}

//reset state
webView.getTransforms().remove(activePage);

complete.set(true);
});
}
catch(Exception e) { thrown.set(e); }
});
Expand Down

0 comments on commit 8024139

Please sign in to comment.