Skip to content

Commit

Permalink
Merge pull request #8 from DEXPRO-Solutions-GmbH/fix/landscape-scaling
Browse files Browse the repository at this point in the history
Fix landscape scaling (due to hardcoded A4 page mediabox)
  • Loading branch information
fabiante authored Oct 12, 2023
2 parents c900524 + e979b47 commit eb387d5
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ build/
.vscode/

### Mac OS ###
.DS_Store
.DS_Store
/tmp/
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>one.squeeze</groupId>
<artifactId>pdftools</artifactId>
<version>0.1.1</version>
<version>0.1.2</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/one/squeeze/pdftools/DIN.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package one.squeeze.pdftools;

import org.apache.pdfbox.pdmodel.common.PDRectangle;

/**
* This class holds constants for DIN paper sizes.
*/
abstract public class DIN {

public static final PDRectangle A4 = PDRectangle.A4;
public static final PDRectangle A4_Landscape = new PDRectangle(A4.getHeight(), A4.getWidth());

}
19 changes: 14 additions & 5 deletions src/main/java/one/squeeze/pdftools/cli/cmds/FixPDFCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package one.squeeze.pdftools.cli.cmds;


import one.squeeze.pdftools.DIN;
import one.squeeze.pdftools.app.scale.IScaler;
import one.squeeze.pdftools.app.scale.NoopScaler;
import one.squeeze.pdftools.app.scale.Scaler;
Expand Down Expand Up @@ -79,15 +80,23 @@ public static IScaler buildScaler(PDPage page) {
float fWidth = 1;
float fHeight = 1;
if (isPortrait) {
fWidth = MAX_WIDTH / page.getMediaBox().getWidth();
fHeight = MAX_HEIGHT / page.getMediaBox().getHeight();
fWidth = MAX_WIDTH / mediaBox.getWidth();
fHeight = MAX_HEIGHT / mediaBox.getHeight();
} else {
fWidth = MAX_HEIGHT / page.getMediaBox().getWidth();
fHeight = MAX_WIDTH / page.getMediaBox().getHeight();
fWidth = MAX_HEIGHT / mediaBox.getWidth();
fHeight = MAX_WIDTH / mediaBox.getHeight();
}

float factor = Math.min(fWidth, fHeight);

return new Scaler(factor, PDRectangle.A4);
// Determine new media box
PDRectangle targetMediaBox;
if (isPortrait) {
targetMediaBox = DIN.A4;
} else {
targetMediaBox = DIN.A4_Landscape;
}

return new Scaler(factor, targetMediaBox);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package one.squeeze.pdftools.cli.cmds;

import one.squeeze.pdftools.DIN;
import one.squeeze.pdftools.app.scale.IScaler;
import one.squeeze.pdftools.app.scale.NoopScaler;
import one.squeeze.pdftools.app.scale.Scaler;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class FixPDFCommandTest {

Expand Down Expand Up @@ -77,7 +79,7 @@ void testBuildScaler_ScalesLargePortrait() {

Scaler scaler = (Scaler) FixPDFCommand.buildScaler(page);
assertEquals(TEN_PERCENT, scaler.getFactor());
assertEquals(PDRectangle.A4, scaler.getTargetMediaBox());
assertEquals(DIN.A4, scaler.getTargetMediaBox());
}

@Test
Expand All @@ -90,6 +92,6 @@ void testBuildScaler_ScalesLargeLandscape() {

Scaler scaler = (Scaler) FixPDFCommand.buildScaler(page);
assertEquals(TEN_PERCENT, scaler.getFactor());
assertEquals(PDRectangle.A4, scaler.getTargetMediaBox());
assertEquals(DIN.A4_Landscape, scaler.getTargetMediaBox());
}
}
Binary file added testfiles/a2-landscape.pdf
Binary file not shown.
Binary file added testfiles/a2.pdf
Binary file not shown.
Binary file added testfiles/a3-landscape.pdf
Binary file not shown.
Binary file added testfiles/a3.pdf
Binary file not shown.
Binary file added testfiles/a5-landscape.pdf
Binary file not shown.
Binary file added testfiles/a5.pdf
Binary file not shown.

0 comments on commit eb387d5

Please sign in to comment.