Generate a preview without print margins

When a PDF is used for both printing and displaying. It will most likely
contain a crop box in order to hide print margins when displaying the PDF.

Use Poppler's parameter to automatically use the crop box (visible box)
rather than the media box (printable box) in order to remove those margins
when drawing the PDF.

See https://manpages.debian.org/testing/poppler-utils/pdftoppm.1.en.html
This commit is contained in:
Vincent Robert 2020-09-16 16:18:53 +02:00 committed by George Claghorn
parent 59ef3c8e70
commit 3803671a81
6 changed files with 52 additions and 11 deletions

View File

@ -1,3 +1,9 @@
* The Poppler PDF previewer renders a preview image using the original
document's crop box rather than its media box, hiding print margins. This
matches the behavior of the MuPDF previewer.
*Vincent Robert*
* Touch parent model when an attachment is purged.
*Víctor Pérez Rodríguez*

View File

@ -29,7 +29,7 @@ module ActiveStorage
private
def draw_first_page_from(file, &block)
# use 72 dpi to match thumbnail dimensions of the PDF
draw self.class.pdftoppm_path, "-singlefile", "-r", "72", "-png", file.path, &block
draw self.class.pdftoppm_path, "-singlefile", "-cropbox", "-r", "72", "-png", file.path, &block
end
end
end

Binary file not shown.

View File

@ -17,6 +17,19 @@ class ActiveStorage::PreviewTest < ActiveSupport::TestCase
assert_equal 792, image.height
end
test "previewing a cropped PDF" do
blob = create_file_blob(filename: "cropped.pdf", content_type: "application/pdf")
preview = blob.preview(resize: "640x280").processed
assert_predicate preview.image, :attached?
assert_equal "cropped.png", preview.image.filename.to_s
assert_equal "image/png", preview.image.content_type
image = read_image(preview.image)
assert_equal 430, image.width
assert_equal 145, image.height
end
test "previewing an MP4 video" do
blob = create_file_blob(filename: "video.mp4", content_type: "video/mp4")
preview = blob.preview(resize: "640x280").processed

View File

@ -6,12 +6,10 @@ require "database/setup"
require "active_storage/previewer/mupdf_previewer"
class ActiveStorage::Previewer::MuPDFPreviewerTest < ActiveSupport::TestCase
setup do
@blob = create_file_blob(filename: "report.pdf", content_type: "application/pdf")
end
test "previewing a PDF document" do
ActiveStorage::Previewer::MuPDFPreviewer.new(@blob).preview do |attachable|
blob = create_file_blob(filename: "report.pdf", content_type: "application/pdf")
ActiveStorage::Previewer::MuPDFPreviewer.new(blob).preview do |attachable|
assert_equal "image/png", attachable[:content_type]
assert_equal "report.png", attachable[:filename]
@ -20,4 +18,17 @@ class ActiveStorage::Previewer::MuPDFPreviewerTest < ActiveSupport::TestCase
assert_equal 792, image.height
end
end
test "previewing a cropped PDF document" do
blob = create_file_blob(filename: "cropped.pdf", content_type: "application/pdf")
ActiveStorage::Previewer::MuPDFPreviewer.new(blob).preview do |attachable|
assert_equal "image/png", attachable[:content_type]
assert_equal "cropped.png", attachable[:filename]
image = MiniMagick::Image.read(attachable[:io])
assert_equal 430, image.width
assert_equal 145, image.height
end
end
end

View File

@ -6,12 +6,10 @@ require "database/setup"
require "active_storage/previewer/poppler_pdf_previewer"
class ActiveStorage::Previewer::PopplerPDFPreviewerTest < ActiveSupport::TestCase
setup do
@blob = create_file_blob(filename: "report.pdf", content_type: "application/pdf")
end
test "previewing a PDF document" do
ActiveStorage::Previewer::PopplerPDFPreviewer.new(@blob).preview do |attachable|
blob = create_file_blob(filename: "report.pdf", content_type: "application/pdf")
ActiveStorage::Previewer::PopplerPDFPreviewer.new(blob).preview do |attachable|
assert_equal "image/png", attachable[:content_type]
assert_equal "report.png", attachable[:filename]
@ -20,4 +18,17 @@ class ActiveStorage::Previewer::PopplerPDFPreviewerTest < ActiveSupport::TestCas
assert_equal 792, image.height
end
end
test "previewing a cropped PDF document" do
blob = create_file_blob(filename: "cropped.pdf", content_type: "application/pdf")
ActiveStorage::Previewer::PopplerPDFPreviewer.new(blob).preview do |attachable|
assert_equal "image/png", attachable[:content_type]
assert_equal "cropped.png", attachable[:filename]
image = MiniMagick::Image.read(attachable[:io])
assert_equal 430, image.width
assert_equal 145, image.height
end
end
end