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

     1  /*
     2  	Copyright 2020 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/pkg/errors"
    27  )
    28  
    29  // Optimize reads a PDF stream from rs and writes the optimized PDF stream to w.
    30  func Optimize(rs io.ReadSeeker, w io.Writer, conf *model.Configuration) error {
    31  	if rs == nil {
    32  		return errors.New("pdfcpu: Optimize: missing rs")
    33  	}
    34  
    35  	if conf == nil {
    36  		conf = model.NewDefaultConfiguration()
    37  	}
    38  
    39  	ctx, err := ReadValidateAndOptimize(rs, conf)
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	if log.StatsEnabled() {
    45  		log.Stats.Printf("XRefTable:\n%s\n", ctx)
    46  	}
    47  
    48  	if err = WriteContext(ctx, w); err != nil {
    49  		return err
    50  	}
    51  
    52  	// For Optimize only.
    53  	if ctx.StatsFileName != "" {
    54  		err = pdfcpu.AppendStatsFile(ctx)
    55  		if err != nil {
    56  			return errors.Wrap(err, "Write stats failed.")
    57  		}
    58  	}
    59  
    60  	return nil
    61  }
    62  
    63  // OptimizeFile reads inFile and writes the optimized PDF to outFile.
    64  // If outFile is not provided then inFile gets overwritten
    65  // which leads to the same result as when inFile equals outFile.
    66  func OptimizeFile(inFile, outFile string, conf *model.Configuration) (err error) {
    67  	var f1, f2 *os.File
    68  
    69  	if f1, err = os.Open(inFile); err != nil {
    70  		return err
    71  	}
    72  
    73  	tmpFile := inFile + ".tmp"
    74  	if outFile != "" && inFile != outFile {
    75  		tmpFile = outFile
    76  		logWritingTo(outFile)
    77  	} else {
    78  		logWritingTo(inFile)
    79  	}
    80  
    81  	if f2, err = os.Create(tmpFile); err != nil {
    82  		return err
    83  	}
    84  
    85  	defer func() {
    86  		if err != nil {
    87  			f2.Close()
    88  			f1.Close()
    89  			os.Remove(tmpFile)
    90  			return
    91  		}
    92  		if err = f2.Close(); err != nil {
    93  			return
    94  		}
    95  		if err = f1.Close(); err != nil {
    96  			return
    97  		}
    98  		if outFile == "" || inFile == outFile {
    99  			err = os.Rename(tmpFile, inFile)
   100  		}
   101  	}()
   102  
   103  	if conf == nil {
   104  		conf = model.NewDefaultConfiguration()
   105  	}
   106  	conf.Cmd = model.OPTIMIZE
   107  
   108  	return Optimize(f1, f2, conf)
   109  }