Skip to content

Commit

Permalink
(#22) Fix trailer size
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriciofx committed Dec 7, 2023
1 parent dfb4a2c commit 11a2e49
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
Binary file added HelloWorld.pdf
Binary file not shown.
18 changes: 15 additions & 3 deletions src/main/java/com/github/fabriciofx/cactoos/pdf/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public final class Document implements Bytes {
*/
private static final String EOF = "%%%%EOF";

/**
* Count.
*/
private final Count count;

/**
* PDF metadata.
*/
Expand All @@ -65,10 +70,16 @@ public final class Document implements Bytes {
/**
* Ctor.
*
* @param count Count
* @param metadata Metadata
* @param catalog Catalog
*/
public Document(final Metadata metadata, final Catalog catalog) {
public Document(
final Count count,
final Metadata metadata,
final Catalog catalog
) {
this.count = count;
this.metadata = metadata;
this.catalog = catalog;
}
Expand All @@ -87,8 +98,9 @@ public byte[] asBytes() throws Exception {
baos.write(this.catalog.asBytes());
baos.write(
new FormattedText(
"trailer << /Root %s /Size 6 >>\n",
this.catalog.reference()
"trailer << /Root %s /Size %d >>\n",
this.catalog.reference(),
this.count.value()
).asString().getBytes()
);
baos.write(Document.EOF.getBytes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ void buildDocument() {
"Must represent a PDF document",
new TextOf(
new Document(
count,
new Metadata(count, "Hello World"),
new Catalog(
count,
Expand Down Expand Up @@ -84,7 +85,7 @@ void buildDocument() {
"4 0 obj\n<< /Type /Page /Resources 2 0 R /Contents 3 0 R /Parent 5 0 R >>\nendobj",
"2 0 obj\n<< /Font << /F1 << /Type /Font /BaseFont /Times-Roman /Subtype /Type1 >> >> >>\nendobj",
"3 0 obj\n<< /Length 62 >>\nstream\nBT /F1 18 Tf 0 0 Td (Hello World with \\(, \\), \\\\ and \\r) Tj ET\nendstream\nendobj",
"trailer << /Root 6 0 R /Size 6 >>",
"trailer << /Root 6 0 R /Size 7 >>",
"%%%%EOF"
)
)
Expand All @@ -99,6 +100,7 @@ void buildFile() throws Exception {
Files.write(
file.toPath(),
new Document(
count,
new Metadata(count, "Hello World"),
new Catalog(
count,
Expand Down

0 comments on commit 11a2e49

Please sign in to comment.