Added image downsampling/compression
All checks were successful
Build PDF / make pdf (push) Successful in 6m1s
All checks were successful
Build PDF / make pdf (push) Successful in 6m1s
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"path/filepath"
|
||||
|
||||
"smartbar/internal/config"
|
||||
"smartbar/internal/pdfassets"
|
||||
|
||||
"github.com/chromedp/cdproto/emulation"
|
||||
"github.com/chromedp/cdproto/page"
|
||||
@@ -38,15 +39,19 @@ func findChromeExec() (string, error) {
|
||||
|
||||
func main() {
|
||||
var (
|
||||
input string
|
||||
output string
|
||||
width float64
|
||||
height float64
|
||||
input string
|
||||
output string
|
||||
width float64
|
||||
height float64
|
||||
imageDPI int
|
||||
jpegQuality int
|
||||
)
|
||||
flag.StringVar(&input, "in", "", "input HTML file path (required)")
|
||||
flag.StringVar(&output, "out", "", "output PDF path (required)")
|
||||
flag.Float64Var(&width, "w", config.PageWidthIn, "page width in inches")
|
||||
flag.Float64Var(&height, "h", config.PageHeightIn, "page height in inches")
|
||||
flag.IntVar(&imageDPI, "image-dpi", 144, "downsample image assets to this PDF pixel density; 0 disables")
|
||||
flag.IntVar(&jpegQuality, "jpeg-quality", 75, "JPEG quality for PDF image assets")
|
||||
flag.Parse()
|
||||
|
||||
if input == "" || output == "" {
|
||||
@@ -54,6 +59,32 @@ func main() {
|
||||
}
|
||||
|
||||
absInput, _ := filepath.Abs(input)
|
||||
inputRoot := filepath.Dir(absInput)
|
||||
inputRel := filepath.Base(absInput)
|
||||
imageOpts := pdfassets.OptionsForPage(width, height, imageDPI, jpegQuality)
|
||||
prepared, err := pdfassets.PrepareTree(inputRoot, imageOpts)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
if err := prepared.Cleanup(); err != nil {
|
||||
log.Printf("warning: cleanup optimized PDF assets: %v", err)
|
||||
}
|
||||
}()
|
||||
if prepared.Stats.ImageFiles > 0 {
|
||||
fmt.Printf(
|
||||
"Optimized PDF images: %d/%d files, %s -> %s (max %dx%d, JPEG quality %d)\n",
|
||||
prepared.Stats.OptimizedImages,
|
||||
prepared.Stats.ImageFiles,
|
||||
pdfassets.FormatBytes(prepared.Stats.OriginalBytes),
|
||||
pdfassets.FormatBytes(prepared.Stats.OutputBytes),
|
||||
imageOpts.MaxWidth,
|
||||
imageOpts.MaxHeight,
|
||||
imageOpts.JPEGQuality,
|
||||
)
|
||||
}
|
||||
|
||||
absInput = filepath.Join(prepared.Root, inputRel)
|
||||
url := "file://" + absInput
|
||||
|
||||
execPath, err := findChromeExec()
|
||||
|
||||
Reference in New Issue
Block a user