github.com/pdfcpu/pdfcpu@v0.11.1/pkg/api/booklet.go (about)

     1  /*
     2  	Copyright 2021 The pdfcpu Authors.
     3  
     4  	Licensed under the Apache License, Version 2.0 (the "License");
     5  	you may not use this file except in compliance with the License.
     6  	You may obtain a copy of the License at
     7  
     8  		http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  	Unless required by applicable law or agreed to in writing, software
    11  	distributed under the License is distributed on an "AS IS" BASIS,
    12  	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  	See the License for the specific language governing permissions and
    14  	limitations under the License.
    15  */
    16  
    17  package api
    18  
    19  import (
    20  	"io"
    21  	"os"
    22  
    23  	"github.com/pdfcpu/pdfcpu/pkg/log"
    24  	"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
    25  	"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
    26  	"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types"
    27  	"github.com/pkg/errors"
    28  )
    29  
    30  // BookletFromImages creates a booklet from images.
    31  func BookletFromImages(conf *model.Configuration, imageFileNames []string, nup *model.NUp) (*model.Context, error) {
    32  	if nup.PageDim == nil {
    33  		// Set default paper size.
    34  		nup.PageDim = types.PaperSize[nup.PageSize]
    35  	}
    36  
    37  	ctx, err := pdfcpu.CreateContextWithXRefTable(conf, nup.PageDim)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  
    42  	pagesIndRef, err := ctx.Pages()
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	// This is the page tree root.
    48  	pagesDict, err := ctx.DereferenceDict(*pagesIndRef)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	err = pdfcpu.BookletFromImages(ctx, imageFileNames, nup, pagesDict, pagesIndRef)
    54  
    55  	return ctx, err
    56  }
    57  
    58  // Booklet arranges PDF pages on larger sheets of paper and writes the result to w.
    59  func Booklet(rs io.ReadSeeker, w io.Writer, imgFiles, selectedPages []string, nup *model.NUp, conf *model.Configuration) error {
    60  	if rs == nil {
    61  		return errors.New("pdfcpu: Booklet: missing rs")
    62  	}
    63  
    64  	if conf == nil {
    65  		conf = model.NewDefaultConfiguration()
    66  	}
    67  	conf.Cmd = model.BOOKLET
    68  
    69  	if log.InfoEnabled() {
    70  		log.Info.Printf("%s", nup)
    71  	}
    72  
    73  	var (
    74  		ctx *model.Context
    75  		err error
    76  	)
    77  
    78  	if nup.ImgInputFile {
    79  
    80  		if ctx, err = BookletFromImages(conf, imgFiles, nup); err != nil {
    81  			return err
    82  		}
    83  
    84  	} else {
    85  
    86  		if ctx, err = ReadAndValidate(rs, conf); err != nil {
    87  			return err
    88  		}
    89  
    90  		pages, err := PagesForPageSelection(ctx.PageCount, selectedPages, true, true)
    91  		if err != nil {
    92  			return err
    93  		}
    94  
    95  		if err = pdfcpu.BookletFromPDF(ctx, pages, nup); err != nil {
    96  			return err
    97  		}
    98  	}
    99  
   100  	return Write(ctx, w, conf)
   101  }
   102  
   103  // BookletFile rearranges PDF pages or images into a booklet layout and writes the result to outFile.
   104  func BookletFile(inFiles []string, outFile string, selectedPages []string, nup *model.NUp, conf *model.Configuration) (err error) {
   105  	var f1, f2 *os.File
   106  
   107  	// booklet from a PDF
   108  	if f1, err = os.Open(inFiles[0]); err != nil {
   109  		return err
   110  	}
   111  
   112  	if f2, err = os.Create(outFile); err != nil {
   113  		f1.Close()
   114  		return err
   115  	}
   116  	logWritingTo(outFile)
   117  
   118  	defer func() {
   119  		if err != nil {
   120  			f2.Close()
   121  			f1.Close()
   122  			return
   123  		}
   124  		if err = f2.Close(); err != nil {
   125  			return
   126  		}
   127  		err = f1.Close()
   128  	}()
   129  
   130  	return Booklet(f1, f2, inFiles, selectedPages, nup, conf)
   131  }