mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-10-29 15:36:36 +08:00
Switch to pdftoppm PDF preview maker
This commit is contained in:
parent
a20a0ed5e2
commit
03d7affaae
1 changed files with 17 additions and 12 deletions
|
|
@ -3,6 +3,7 @@
|
||||||
module Paperclip
|
module Paperclip
|
||||||
class CustomFilePreview < Processor
|
class CustomFilePreview < Processor
|
||||||
def make
|
def make
|
||||||
|
pdftoppm_path = ENV['PDFTOPPM_PATH'] || 'pdftoppm'
|
||||||
libreoffice_path = ENV['LIBREOFFICE_PATH'] || 'soffice'
|
libreoffice_path = ENV['LIBREOFFICE_PATH'] || 'soffice'
|
||||||
directory = File.dirname(@file.path)
|
directory = File.dirname(@file.path)
|
||||||
basename = File.basename(@file.path, '.*')
|
basename = File.basename(@file.path, '.*')
|
||||||
|
|
@ -11,25 +12,29 @@ module Paperclip
|
||||||
|
|
||||||
begin
|
begin
|
||||||
if @file.content_type == 'application/pdf'
|
if @file.content_type == 'application/pdf'
|
||||||
# We use special convert options for PDFs to improve quality and
|
content = Paperclip.run(
|
||||||
# background, we append [0] to convert only the first page
|
pdftoppm_path,
|
||||||
convert(
|
"-singlefile -r 72 -png #{@file.path}"
|
||||||
":source -resize '#{options[:geometry]}' -format #{options[:format]} -flatten -quality 70 :dest",
|
|
||||||
source: File.expand_path(@file.path) + '[0]',
|
|
||||||
dest: File.expand_path(dst.path)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Save intermediate content to tempfile
|
||||||
|
tmp = TempfileFactory.new.generate
|
||||||
|
tmp.write(content)
|
||||||
|
tmp.rewind
|
||||||
|
|
||||||
|
original_preview_file = tmp.path
|
||||||
else
|
else
|
||||||
Paperclip.run(
|
Paperclip.run(
|
||||||
libreoffice_path,
|
libreoffice_path,
|
||||||
"--headless --invisible --convert-to png --outdir #{directory} #{@file.path}"
|
"--headless --invisible --convert-to png --outdir #{directory} #{@file.path}"
|
||||||
)
|
)
|
||||||
|
|
||||||
convert(
|
|
||||||
":source -resize '#{options[:geometry]}' -format #{options[:format]} #{options[:convert_options]} :dest",
|
|
||||||
source: File.expand_path(original_preview_file),
|
|
||||||
dest: File.expand_path(dst.path)
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
convert(
|
||||||
|
":source -resize '#{options[:geometry]}' -format #{options[:format]} #{options[:convert_options]} :dest",
|
||||||
|
source: File.expand_path(original_preview_file),
|
||||||
|
dest: File.expand_path(dst.path)
|
||||||
|
)
|
||||||
ensure
|
ensure
|
||||||
File.delete(original_preview_file) if File.file?(original_preview_file)
|
File.delete(original_preview_file) if File.file?(original_preview_file)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue