diff --git a/activestorage/CHANGELOG.md b/activestorage/CHANGELOG.md index 4a91a8e070e02b66380e8be123d10b68fad68a75..51fe89c8296d602c4c88ce6e17255f65869a188d 100644 --- a/activestorage/CHANGELOG.md +++ b/activestorage/CHANGELOG.md @@ -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* diff --git a/activestorage/lib/active_storage/previewer/poppler_pdf_previewer.rb b/activestorage/lib/active_storage/previewer/poppler_pdf_previewer.rb index c7f9136983d9c7d440e9939f433b2a28e4c9258c..8eaffc64914b53f5006b8252aafc001144ad332e 100644 --- a/activestorage/lib/active_storage/previewer/poppler_pdf_previewer.rb +++ b/activestorage/lib/active_storage/previewer/poppler_pdf_previewer.rb @@ -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 diff --git a/activestorage/test/fixtures/files/cropped.pdf b/activestorage/test/fixtures/files/cropped.pdf new file mode 100644 index 0000000000000000000000000000000000000000..8a54a1008df20ac78bcc95ad9fd68f1e87c35a87 Binary files /dev/null and b/activestorage/test/fixtures/files/cropped.pdf differ diff --git a/activestorage/test/models/preview_test.rb b/activestorage/test/models/preview_test.rb index 87d2b6b0ec94fb1db0c3666122f0a9aa2f11aca7..90e5b092faacd4cfd1f2aa310e69320875fcb008 100644 --- a/activestorage/test/models/preview_test.rb +++ b/activestorage/test/models/preview_test.rb @@ -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 diff --git a/activestorage/test/previewer/mupdf_previewer_test.rb b/activestorage/test/previewer/mupdf_previewer_test.rb index 6c2db6fcbf4db943f23f19e26431fb6172d3df66..65869dfe37c72102e5fef7eac148a37b5268fdf5 100644 --- a/activestorage/test/previewer/mupdf_previewer_test.rb +++ b/activestorage/test/previewer/mupdf_previewer_test.rb @@ -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 diff --git a/activestorage/test/previewer/poppler_pdf_previewer_test.rb b/activestorage/test/previewer/poppler_pdf_previewer_test.rb index 2b41c8b642abc4668a763923db0138069c9e6291..5809b5a058976081b0367e94060537f52248e65c 100644 --- a/activestorage/test/previewer/poppler_pdf_previewer_test.rb +++ b/activestorage/test/previewer/poppler_pdf_previewer_test.rb @@ -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