Skip to content

Commit

Permalink
Fix #663
Browse files Browse the repository at this point in the history
  • Loading branch information
hhrutter committed Jul 31, 2023
1 parent 3161cdf commit a9afcfe
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
9 changes: 5 additions & 4 deletions pkg/api/merge.go
Expand Up @@ -116,7 +116,7 @@ func Merge(destFile string, inFiles []string, w io.Writer, conf *model.Configura
}

if conf.CreateBookmarks {
if err := pdfcpu.EnsureOutlines(ctxDest, filepath.Base(destFile)); err != nil {
if err := pdfcpu.EnsureOutlines(ctxDest, filepath.Base(destFile), conf.Cmd == model.MERGEAPPEND); err != nil {
return err
}
}
Expand Down Expand Up @@ -190,11 +190,11 @@ func MergeAppendFile(inFiles []string, outFile string, conf *model.Configuration

defer func() {
if err != nil {
if err = f.Close(); err != nil {
if err1 := f.Close(); err1 != nil {
return
}
if overWrite {
err = os.Remove(tmpFile)
os.Remove(tmpFile)
}
return
}
Expand All @@ -206,5 +206,6 @@ func MergeAppendFile(inFiles []string, outFile string, conf *model.Configuration
}
}()

return Merge(destFile, inFiles, f, conf)
err = Merge(destFile, inFiles, f, conf)
return err
}
12 changes: 8 additions & 4 deletions pkg/api/test/merge_test.go
Expand Up @@ -63,6 +63,12 @@ func TestMergeAppendNew(t *testing.T) {
if err := api.MergeAppendFile(inFiles, outFile, nil); err != nil {
t.Fatalf("%s: %v\n", msg, err)
}

anotherFile := filepath.Join(inDir, "testRot.pdf")
err := api.MergeAppendFile([]string{anotherFile}, outFile, nil)
if err != nil {
t.Fatalf("%s: %v\n", msg, err)
}
}

func TestMergeToBufNew(t *testing.T) {
Expand Down Expand Up @@ -92,6 +98,7 @@ func TestMergeRaw(t *testing.T) {
filepath.Join(inDir, "Acroforms2.pdf"),
filepath.Join(inDir, "adobe_errata.pdf"),
}
outFile := filepath.Join(outDir, "test.pdf")

var rsc []io.ReadSeeker = make([]io.ReadSeeker, 2)

Expand All @@ -104,15 +111,12 @@ func TestMergeRaw(t *testing.T) {

f1, err := os.Open(inFiles[1])
if err != nil {
t.Fatalf("%s: open file1: %v\n", msg, err)
t.Fatalf("%s: open file2: %v\n", msg, err)
}
defer f1.Close()
rsc[1] = f1

outFile := filepath.Join(outDir, "test.pdf")

buf := &bytes.Buffer{}

if err := api.MergeRaw(rsc, buf, nil); err != nil {
t.Fatalf("%s: merge: %v\n", msg, err)
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/pdfcpu/merge.go
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types"
)

func EnsureOutlines(ctx *model.Context, fName string) error {
func EnsureOutlines(ctx *model.Context, fName string, append bool) error {

rootDict, err := ctx.Catalog()
if err != nil {
Expand All @@ -51,6 +51,9 @@ func EnsureOutlines(ctx *model.Context, fName string) error {
outlinesDict["Count"] = types.Integer(total + visible)

if obj, ok := rootDict.Find("Outlines"); ok {
if append {
return nil
}
d, err := ctx.DereferenceDict(obj)
if err != nil {
return err
Expand Down

0 comments on commit a9afcfe

Please sign in to comment.