提交 3803671a 编写于 作者: V Vincent Robert 提交者: George Claghorn

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
上级 59ef3c8e
* 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*
......
......@@ -29,7 +29,7 @@ def preview(**options)
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
......@@ -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
......
......@@ -6,12 +6,10 @@
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
......@@ -6,12 +6,10 @@
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册