Skip to content

Commit

Permalink
Fix invisible cross in PDF QR code
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbl committed Aug 30, 2023
1 parent 9dd24e2 commit f2c71a2
Show file tree
Hide file tree
Showing 21 changed files with 49 additions and 9 deletions.
49 changes: 40 additions & 9 deletions generator/src/main/java/net/codecrete/qrbill/canvas/PDFCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;

Expand Down Expand Up @@ -48,7 +49,6 @@ public class PDFCanvas extends AbstractCanvas implements ByteArrayResult {
private int lastNonStrokingColor = 0;
private double lastLineWidth = 1;
private LineStyle lastLineStyle = LineStyle.Solid;
private boolean hasSavedGraphicsState = false;

/**
* Creates a new instance using the specified page size.
Expand All @@ -75,6 +75,7 @@ public PDFCanvas(double width, double height) throws IOException {
document.addPage(page);
contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.OVERWRITE, true);
isContentStreamOwned = true;
initGraphicsState();
}

/**
Expand Down Expand Up @@ -104,6 +105,7 @@ public PDFCanvas(Path path, int pageNo) throws IOException {
document = PDDocument.load(Files.newInputStream(path));
preparePage(document, pageNo);
isContentStreamOwned = true;
initGraphicsState();
}

/**
Expand Down Expand Up @@ -133,6 +135,7 @@ public PDFCanvas(byte[] pdfDocument, int pageNo) throws IOException {
document = PDDocument.load(pdfDocument);
preparePage(document, pageNo);
isContentStreamOwned = true;
initGraphicsState();
}

/**
Expand Down Expand Up @@ -163,6 +166,7 @@ public PDFCanvas(PDDocument pdfDocument, int pageNo) throws IOException {
setupFontMetrics(PDF_FONT);
preparePage(pdfDocument, pageNo);
isContentStreamOwned = true;
initGraphicsState();
}

/**
Expand All @@ -186,6 +190,12 @@ public PDFCanvas(PDPageContentStream contentStream) {
setupFontMetrics(PDF_FONT);
this.contentStream = contentStream;
isContentStreamOwned = false;
try {
initGraphicsState();
} catch (IOException e) {
// should add throws IOException to constructor in next major release
throw new UncheckedIOException(e);
}
}

private void preparePage(PDDocument doc, int pageNo) throws IOException {
Expand All @@ -200,20 +210,36 @@ private void preparePage(PDDocument doc, int pageNo) throws IOException {
}
}

private void initGraphicsState() throws IOException {
if (!isContentStreamOwned) {
// save initial graphics state to restore it after drawing
// (not needed if the content stream is owned as it will be closed)
contentStream.saveGraphicsState();

// set default graphics state
contentStream.setStrokingColor(0.0f, 0.0f, 0.0f);
contentStream.setNonStrokingColor(0.0f, 0.0f, 0.0f);
contentStream.setLineWidth(1);
contentStream.setLineJoinStyle(0);
contentStream.setLineCapStyle(0);
contentStream.setLineDashPattern(new float[0], 0);
contentStream.setMiterLimit(10);
}

contentStream.saveGraphicsState();
}

@Override
public void setTransformation(double translateX, double translateY, double rotate, double scaleX, double scaleY) throws IOException {
translateX *= MM_TO_PT;
translateY *= MM_TO_PT;

if (hasSavedGraphicsState) {
contentStream.restoreGraphicsState();
lastStrokingColor = 0;
lastNonStrokingColor = 0;
lastLineWidth = 1;
}
contentStream.restoreGraphicsState();
lastStrokingColor = 0;
lastNonStrokingColor = 0;
lastLineWidth = 1;

contentStream.saveGraphicsState();
hasSavedGraphicsState = true;
Matrix matrix = new Matrix();
matrix.translate((float) translateX, (float) translateY);
if (rotate != 0) matrix.rotate(rotate);
Expand Down Expand Up @@ -416,9 +442,14 @@ public void close() throws IOException {

private void closeContentStream() throws IOException {
if (contentStream != null) {
contentStream.restoreGraphicsState();

if (isContentStreamOwned) {
contentStream.close();
} else if (hasSavedGraphicsState) {
} else {
// restore to default state
contentStream.restoreGraphicsState();
// restore to state before creating this instance
contentStream.restoreGraphicsState();
}
contentStream = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,13 @@ void qrCodeWithQuietZone() {
byte[] svg = QRBill.generate(bill);
FileComparison.assertFileContentsEqual(svg, "qrcode_quiet_zone.svg");
}

@Test
void qrCodeWithQuietZonePDF() {
Bill bill = SampleData.getExample3();
bill.getFormat().setOutputSize(OutputSize.QR_CODE_WITH_QUIET_ZONE);
bill.getFormat().setGraphicsFormat(GraphicsFormat.PDF);
byte[] svg = QRBill.generate(bill);
FileComparison.assertFileContentsEqual(svg, "qrcode_quiet_zone.pdf");
}
}
Binary file modified generator/src/test/resources/a4bill_ex1.pdf
Binary file not shown.
Binary file modified generator/src/test/resources/a4bill_ex2.pdf
Binary file not shown.
Binary file modified generator/src/test/resources/a4bill_ex3.pdf
Binary file not shown.
Binary file modified generator/src/test/resources/a4bill_ex4.pdf
Binary file not shown.
Binary file modified generator/src/test/resources/a4bill_ex5.pdf
Binary file not shown.
Binary file modified generator/src/test/resources/a4bill_ex6.pdf
Binary file not shown.
Binary file modified generator/src/test/resources/a4bill_postproc1.pdf
Binary file not shown.
Binary file modified generator/src/test/resources/a4bill_postproc2.pdf
Binary file not shown.
Binary file modified generator/src/test/resources/invoice-01.pdf
Binary file not shown.
Binary file modified generator/src/test/resources/invoice-02.pdf
Binary file not shown.
Binary file modified generator/src/test/resources/invoice-03.pdf
Binary file not shown.
Binary file modified generator/src/test/resources/invoice-04.pdf
Binary file not shown.
Binary file modified generator/src/test/resources/linestyle_1.pdf
Binary file not shown.
Binary file modified generator/src/test/resources/linestyle_2.pdf
Binary file not shown.
Binary file modified generator/src/test/resources/pdfcanvas-opendoc.pdf
Binary file not shown.
Binary file modified generator/src/test/resources/pdfcanvas-openstream.pdf
Binary file not shown.
Binary file modified generator/src/test/resources/pdfcanvas-saveas.pdf
Binary file not shown.
Binary file modified generator/src/test/resources/pdfcanvas-writeto.pdf
Binary file not shown.
Binary file not shown.

0 comments on commit f2c71a2

Please sign in to comment.