diff --git a/activestorage/CHANGELOG.md b/activestorage/CHANGELOG.md index 4a91a8e070e..51fe89c8296 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 c7f9136983d..8eaffc64914 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 @@ 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 diff --git a/activestorage/test/fixtures/files/cropped.pdf b/activestorage/test/fixtures/files/cropped.pdf new file mode 100644 index 00000000000..8a54a1008df 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 87d2b6b0ec9..90e5b092faa 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 6c2db6fcbf4..65869dfe37c 100644 --- a/activestorage/test/previewer/mupdf_previewer_test.rb +++ b/activestorage/test/previewer/mupdf_previewer_test.rb @@ -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 diff --git a/activestorage/test/previewer/poppler_pdf_previewer_test.rb b/activestorage/test/previewer/poppler_pdf_previewer_test.rb index 2b41c8b642a..5809b5a0589 100644 --- a/activestorage/test/previewer/poppler_pdf_previewer_test.rb +++ b/activestorage/test/previewer/poppler_pdf_previewer_test.rb @@ -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