-
-
Notifications
You must be signed in to change notification settings - Fork 652
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds PDFBox2 thumbnail renderer; deletes JPedal
- Loading branch information
1 parent
b340017
commit 33c8588
Showing
4 changed files
with
36 additions
and
201 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,20 @@ | ||
require 'java' | ||
require 'observer' | ||
|
||
java.lang.System.setProperty('org.jpedal.jai', 'true') | ||
require_relative './jars/jpedal_lgpl.jar' | ||
require_relative '../lib/jars/tabula-1.0.0-SNAPSHOT-jar-with-dependencies.jar' | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
jeremybmerrill
Author
Member
|
||
|
||
java_import javax.imageio.ImageIO | ||
java_import java.awt.image.BufferedImage | ||
java_import java.awt.Image | ||
|
||
java_import org.jpedal.PdfDecoder | ||
java_import org.jpedal.fonts.FontMappings | ||
java_import org.apache.pdfbox.rendering.PDFRenderer | ||
java_import org.apache.pdfbox.pdmodel.PDDocument | ||
java_import java.io.ByteArrayOutputStream | ||
|
||
|
||
class AbstractThumbnailGenerator | ||
include Observable | ||
SIZE = 800 | ||
|
||
def initialize(pdf_filename, output_directory, sizes=[2048, 560]) | ||
raise Errno::ENOENT unless File.directory?(output_directory) | ||
|
@@ -48,41 +50,38 @@ def generate_thumbnails! | |
end | ||
end | ||
|
||
class JPedalThumbnailGenerator < AbstractThumbnailGenerator | ||
class PDFBox2ThumbnailGenerator < AbstractThumbnailGenerator | ||
def initialize(pdf_filename, output_directory, sizes=[2048, 560]) | ||
super(pdf_filename, output_directory, sizes) | ||
@decoder = PdfDecoder.new(true) | ||
FontMappings.setFontReplacements | ||
@decoder.openPdfFile(pdf_filename) | ||
@decoder.setExtractionMode(0, 1.0) | ||
@decoder.useHiResScreenDisplay(true) | ||
@pdf_document = PDDocument.load(java.io.File.new(pdf_filename)) | ||
end | ||
|
||
def generate_thumbnails! | ||
total_pages = @decoder.getPageCount | ||
|
||
total_pages.times do |i| | ||
|
||
begin | ||
image = @decoder.getPageAsImage(i+1); | ||
image_w, image_h = image.getWidth, image.getHeight | ||
|
||
@sizes.each do |s| | ||
scale = s.to_f / image_w.to_f | ||
bi = BufferedImage.new(s, image_h * scale, image.getType) | ||
bi.getGraphics.drawImage(image.getScaledInstance(s, image_h * scale, Image::SCALE_SMOOTH), 0, 0, nil) | ||
ImageIO.write(bi, | ||
'png', | ||
java.io.File.new(File.join(@output_directory, | ||
"document_#{s}_#{i+1}.png"))) | ||
changed | ||
notify_observers(i+1, total_pages, "generating page thumbnails...") | ||
end | ||
rescue java.lang.RuntimeException | ||
# TODO What? | ||
end | ||
renderer = PDFRenderer.new(@pdf_document); | ||
total_pages = @pdf_document.get_number_of_pages | ||
|
||
total_pages.times do |pi| | ||
image = renderer.render_image_with_dpi(pi, 75); | ||
imageWidth = image.width # was get_width | ||
imageHeight = image.height # was get_height | ||
scale = SIZE / imageWidth.to_f | ||
|
||
bi = BufferedImage.new(SIZE, (imageHeight * scale).round, image.type); | ||
bi.get_graphics.draw_image(image.get_scaled_instance(SIZE, (imageHeight * scale).round, Image::SCALE_SMOOTH), 0, 0, nil); | ||
|
||
out = ByteArrayOutputStream.new | ||
ImageIO.write(bi, "png", out); | ||
|
||
filename = "document_#{SIZE}_#{pi + 1}.png" | ||
ImageIO.write(bi, | ||
'png', | ||
java.io.File.new(File.join(@output_directory, | ||
filename))) | ||
STDERR.puts "Writing page thumbnail #{filename}" | ||
notify_observers(pi+1, total_pages, "generating page thumbnails...") | ||
end | ||
@decoder.closePdfFile | ||
|
||
@pdf_document.close(); | ||
|
||
end | ||
end | ||
|
||
|
@@ -95,7 +94,8 @@ def update(page, total_pages) | |
end | ||
|
||
#pdftg = JPedalThumbnailGenerator.new(ARGV[0], '/tmp', [560]) | ||
pdftg = MUDrawThumbnailGenerator.new(ARGV[0], '/tmp', [560]) | ||
# pdftg = MUDrawThumbnailGenerator.new(ARGV[0], '/tmp', [560]) | ||
pdftg = PDFBox2ThumbnailGenerator.new(ARGV[0], '/tmp', [560]) | ||
pdftg.add_observer(STDERRProgressReporter.new) | ||
pdftg.generate_thumbnails! | ||
end |
I don't think we need this
require
.