From d8b14db198495d4879b5022b48a6b1ed513c0047 Mon Sep 17 00:00:00 2001 From: Phillip Darrall Date: Fri, 3 Oct 2025 09:50:44 +0100 Subject: [PATCH 1/3] Added ASCII85 decode support --- reader.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/reader.go b/reader.go index 48ad707..4acc633 100644 --- a/reader.go +++ b/reader.go @@ -4,17 +4,21 @@ import ( "bufio" "bytes" "compress/zlib" + "encoding/ascii85" "encoding/binary" "fmt" "io" "io/ioutil" "math" "os" + "regexp" "strconv" "github.com/pkg/errors" ) +var adobeASCII85 = regexp.MustCompile(`<~|~>`) + type PdfReader struct { availableBoxes []string stack []string @@ -1436,6 +1440,10 @@ func (this *PdfReader) rebuildContentStream(content *PdfValue) ([]byte, error) { // Set stream to uncompressed data stream = out.Bytes() + case "/ASCII85Decode": + if stream, err = uncompressASCII85(stream); err != nil { + return nil, err + } default: return nil, errors.New("Unspported filter: " + filters[i].Token) } @@ -1444,6 +1452,15 @@ func (this *PdfReader) rebuildContentStream(content *PdfValue) ([]byte, error) { return stream, nil } +func uncompressASCII85(compressed []byte) ([]byte, error) { + // compressed stream may contain <~ and ~> which are not standard ASCII85. Remove them before decoding + compressed = adobeASCII85.ReplaceAll(compressed, []byte{}) + var out bytes.Buffer + reader := ascii85.NewDecoder(bytes.NewBuffer(compressed)) + io.Copy(&out, reader) + return out.Bytes(), nil +} + func (this *PdfReader) getNumPages() (int, error) { if this.pageCount == 0 { return 0, errors.New("Page count is 0") From f2c110bf4797d950e3e8de6c10efcea0fbc1e9ff Mon Sep 17 00:00:00 2001 From: Phillip Darrall Date: Wed, 8 Apr 2026 09:46:55 +0100 Subject: [PATCH 2/3] Revert "Added ASCII85 decode support" This reverts commit d8b14db198495d4879b5022b48a6b1ed513c0047. --- reader.go | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/reader.go b/reader.go index 4acc633..48ad707 100644 --- a/reader.go +++ b/reader.go @@ -4,21 +4,17 @@ import ( "bufio" "bytes" "compress/zlib" - "encoding/ascii85" "encoding/binary" "fmt" "io" "io/ioutil" "math" "os" - "regexp" "strconv" "github.com/pkg/errors" ) -var adobeASCII85 = regexp.MustCompile(`<~|~>`) - type PdfReader struct { availableBoxes []string stack []string @@ -1440,10 +1436,6 @@ func (this *PdfReader) rebuildContentStream(content *PdfValue) ([]byte, error) { // Set stream to uncompressed data stream = out.Bytes() - case "/ASCII85Decode": - if stream, err = uncompressASCII85(stream); err != nil { - return nil, err - } default: return nil, errors.New("Unspported filter: " + filters[i].Token) } @@ -1452,15 +1444,6 @@ func (this *PdfReader) rebuildContentStream(content *PdfValue) ([]byte, error) { return stream, nil } -func uncompressASCII85(compressed []byte) ([]byte, error) { - // compressed stream may contain <~ and ~> which are not standard ASCII85. Remove them before decoding - compressed = adobeASCII85.ReplaceAll(compressed, []byte{}) - var out bytes.Buffer - reader := ascii85.NewDecoder(bytes.NewBuffer(compressed)) - io.Copy(&out, reader) - return out.Bytes(), nil -} - func (this *PdfReader) getNumPages() (int, error) { if this.pageCount == 0 { return 0, errors.New("Page count is 0") From db351ccadfd6e5044e2b8f41d01412b089bf52a2 Mon Sep 17 00:00:00 2001 From: Phillip Darrall Date: Thu, 28 May 2026 09:54:22 +0100 Subject: [PATCH 3/3] Select correct box from hierarchy --- writer.go | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/writer.go b/writer.go index 0002e3a..53fdd7a 100644 --- a/writer.go +++ b/writer.go @@ -115,6 +115,22 @@ func (this *PdfWriter) ClearImportedObjects() { this.written_objs = make(map[*PdfObjectId][]byte, 0) } +func getValidBoxName(boxes map[string]map[string]float64, name string) string { + if bm, ok := boxes[name]; !ok || len(bm) == 0 { + switch name { + case "/ArtBox": + return getValidBoxName(boxes, "/TrimBox") + case "/TrimBox": + return getValidBoxName(boxes, "/BleedBox") + case "/BleedBox": + return getValidBoxName(boxes, "/CropBox") + case "/CropBox": + return "/MediaBox" + } + } + return name +} + // Create a PdfTemplate object from a page number (e.g. 1) and a boxName (e.g. MediaBox) func (this *PdfWriter) ImportPage(reader *PdfReader, pageno int, boxName string) (int, error) { var err error @@ -129,13 +145,7 @@ func (this *PdfWriter) ImportPage(reader *PdfReader, pageno int, boxName string) } // If requested box name does not exist for this page, use an alternate box - if _, ok := pageBoxes[boxName]; !ok { - if boxName == "/BleedBox" || boxName == "/TrimBox" || boxName == "ArtBox" { - boxName = "/CropBox" - } else if boxName == "/CropBox" { - boxName = "/MediaBox" - } - } + boxName = getValidBoxName(pageBoxes, boxName) // If the requested box name or an alternate box name cannot be found, trigger an error // TODO: Improve error handling