Skip to content

Commit

Permalink
Merge pull request #251 from anovik/images
Browse files Browse the repository at this point in the history
[US-369] Example of creating PDF using lazy mode images
  • Loading branch information
sampila authored May 3, 2024
2 parents 59c11f7 + f3de445 commit 81764f4
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 9 deletions.
2 changes: 1 addition & 1 deletion extract/pdf_extract_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func extractImagesToArchive(inputPath, outputPath string) error {
if err != nil {
return err
}
opt := jpeg.Options{Quality: 100}
opt := jpeg.Options{Quality: 80}
err = jpeg.Encode(imgf, gimg, &opt)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions image/pdf_add_image_to_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ func addImageToPdf(inputPath string, outputPath string, imagePath string, pageNu
encoder := core.NewDCTEncoder()
// The default quality is 75. There is not much difference in the image
// quality between 75 and 100 but the size difference when compressing the
// image stream is signficant.
// encoder.Quality = 100
// image stream is significant so setting quality to 100 is not recommended.
// encoder.Quality = 80
img.SetEncoder(encoder)

// Read the input pdf file.
Expand Down
9 changes: 5 additions & 4 deletions image/pdf_images_to_pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@ func imagesToPdf(inputPaths []string, outputPath string) error {
common.Log.Debug("Error loading image: %v", err)
return err
}
img.ScaleToWidth(612.0)

// Use page width of 612 points, and calculate the height proportionally based on the image.
// Standard PPI is 72 points per inch, thus a width of 8.5"
height := 612.0 * img.Height() / img.Width()
c.SetPageSize(creator.PageSize{612, height})
pageWidth := 612.0
img.ScaleToWidth(pageWidth)

pageHeight := pageWidth * img.Height() / img.Width()
c.SetPageSize(creator.PageSize{pageWidth, pageHeight})
c.NewPage()
img.SetPos(0, 0)
_ = c.Draw(img)
Expand Down
88 changes: 88 additions & 0 deletions image/pdf_images_to_pdf_lazy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Add JPG images from given folder to a PDF file, one image per page using lazy mode.
*
* Run as: go run pdf_images_to_pdf_lazy.go output.pdf image_folder
*/

package main

import (
"fmt"
"log"
"os"
"path"
"strings"

"github.com/unidoc/unipdf/v3/common"
"github.com/unidoc/unipdf/v3/common/license"
"github.com/unidoc/unipdf/v3/creator"
)

func init() {
// Make sure to load your metered License API key prior to using the library.
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
if err != nil {
panic(err)
}
}

func main() {
if len(os.Args) < 3 {
fmt.Printf("Usage: go run pdf_images_to_pdf_lazy.go output.pdf image_folder\n")
os.Exit(1)
}

outputPath := os.Args[1]
inputPath := os.Args[2]

err := imageFolderToPdf(inputPath, outputPath)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}

fmt.Printf("Complete, see output file: %s\n", outputPath)
}

// Images to PDF.
func imageFolderToPdf(inputPath string, outputPath string) error {
c := creator.New()

files, err := os.ReadDir(inputPath)
if err != nil {
log.Fatal(err)
}

for _, imgPath := range files {
if !strings.HasSuffix(imgPath.Name(), ".jpg") {
continue
}
common.Log.Debug("Image: %s", imgPath)

path := path.Join(inputPath, imgPath.Name())

img, err := c.NewImageFromFile(path)
if err != nil {
common.Log.Debug("Error loading image: %v", err)
return err
}
// Set lazy mode for image allows to reduce memory consumption
// Lazy mode is helpful for creating PDF with a lot of images
img.SetLazy(true)

// Use page width of 612 points, and calculate the height proportionally based on the image.
// Standard PPI is 72 points per inch, thus a width of 8.5"
pageWidth := 612.0
img.ScaleToWidth(pageWidth)

pageHeight := pageWidth * img.Height() / img.Width()
c.SetPageSize(creator.PageSize{pageWidth, pageHeight})
c.NewPage()
img.SetPos(0, 0)
_ = c.Draw(img)
}

err = c.WriteToFile(outputPath)
return err
}
2 changes: 1 addition & 1 deletion jbig2/jbig2_decompress.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func main() {
}
defer imgFile.Close()

err = jpeg.Encode(imgFile, img, &jpeg.Options{Quality: 100})
err = jpeg.Encode(imgFile, img, &jpeg.Options{Quality: 80})
if err != nil {
log.Fatalf("Error: %v\n", err)
}
Expand Down
2 changes: 1 addition & 1 deletion jbig2/jbig2_decompress_with_globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func main() {
}
defer imgFile.Close()

err = jpeg.Encode(imgFile, img, &jpeg.Options{Quality: 100})
err = jpeg.Encode(imgFile, img, &jpeg.Options{Quality: 80})
if err != nil {
log.Fatalf("Error: %v\n", err)
}
Expand Down

0 comments on commit 81764f4

Please sign in to comment.