2 Star 0 Fork 1

Deeao/golang-pdfcpu

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
zoom.go 2.37 KB
Copy Edit Raw Blame History
/*
Copyright 2024 The pdfcpu Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package api
import (
"io"
"os"
"gitee.com/deeao/golang-pdfcpu/pkg/log"
"gitee.com/deeao/golang-pdfcpu/pkg/pdfcpu"
"gitee.com/deeao/golang-pdfcpu/pkg/pdfcpu/model"
"github.com/pkg/errors"
)
// Zoom applies resizeConf for selected pages of rs and writes result to w.
func Zoom(rs io.ReadSeeker, w io.Writer, selectedPages []string, zoom *model.Zoom, conf *model.Configuration) error {
if rs == nil {
return errors.New("pdfcpu: Zoom: missing rs")
}
if conf == nil {
conf = model.NewDefaultConfiguration()
}
conf.Cmd = model.ZOOM
ctx, err := ReadValidateAndOptimize(rs, conf)
if err != nil {
return err
}
pages, err := PagesForPageSelection(ctx.PageCount, selectedPages, true, true)
if err != nil {
return err
}
if err = pdfcpu.Zoom(ctx, pages, zoom); err != nil {
return err
}
return Write(ctx, w, conf)
}
// ZoomFile applies zoomConf for selected pages of inFile and writes result to outFile.
func ZoomFile(inFile, outFile string, selectedPages []string, zoom *model.Zoom, conf *model.Configuration) (err error) {
if log.CLIEnabled() {
log.CLI.Printf("zooming %s\n", inFile)
}
tmpFile := inFile + ".tmp"
if outFile != "" && inFile != outFile {
tmpFile = outFile
logWritingTo(outFile)
} else {
logWritingTo(inFile)
}
var (
f1, f2 *os.File
)
if f1, err = os.Open(inFile); err != nil {
return err
}
if f2, err = os.Create(tmpFile); err != nil {
f1.Close()
return err
}
defer func() {
if err != nil {
f2.Close()
f1.Close()
os.Remove(tmpFile)
return
}
if err = f2.Close(); err != nil {
return
}
if err = f1.Close(); err != nil {
return
}
if outFile == "" || inFile == outFile {
err = os.Rename(tmpFile, inFile)
}
}()
if conf == nil {
conf = model.NewDefaultConfiguration()
}
conf.Cmd = model.ZOOM
return Zoom(f1, f2, selectedPages, zoom, conf)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/deeao/golang-pdfcpu.git
git@gitee.com:deeao/golang-pdfcpu.git
deeao
golang-pdfcpu
golang-pdfcpu
v1.0.2

Search