whimsical-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ru...@apache.org
Subject [whimsy] branch master updated: Fix WHIMSY-284: Error converting PNG to PDF
Date Thu, 01 Aug 2019 12:56:46 GMT
This is an automated email from the ASF dual-hosted git repository.

rubys pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/whimsy.git


The following commit(s) were added to refs/heads/master by this push:
     new e5f07a1  Fix WHIMSY-284: Error converting PNG to PDF
e5f07a1 is described below

commit e5f07a10444d1a61818c8e0cb3cfb6b0cc377b22
Author: Sam Ruby <rubys@intertwingly.net>
AuthorDate: Thu Aug 1 08:56:11 2019 -0400

    Fix WHIMSY-284: Error converting PNG to PDF
---
 www/secretary/workbench/models/attachment.rb | 30 ++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)

diff --git a/www/secretary/workbench/models/attachment.rb b/www/secretary/workbench/models/attachment.rb
index f5643ad..57805f6 100644
--- a/www/secretary/workbench/models/attachment.rb
+++ b/www/secretary/workbench/models/attachment.rb
@@ -1,3 +1,5 @@
+require 'open3'
+
 class Attachment
   IMAGE_TYPES = %w(.gif, .jpg, .jpeg, .png)
   attr_reader :headers
@@ -62,9 +64,33 @@ class Attachment
     if IMAGE_TYPES.include? ext or content_type.start_with? 'image/'
       pdf = SafeTempFile.new([safe_name, '.pdf'])
       img2pdf = File.expand_path('../img2pdf', __dir__.untaint).untaint
-      system img2pdf, '--output', pdf.path, file.path
+      stdout, stderr, status = Open3.capture3 img2pdf, '--output', pdf.path,
+        file.path
+
+      # img2pdf will refuse if there is an alpha channel.  If that happens
+      # use imagemagick to remove the alpha channel and try again.
+      unless status.exitstatus == 0
+        if stderr.include? 'remove the alpha channel'
+          tmppng = SafeTempFile.new([safe_name, '.png'])
+          system 'convert', file.path, '-background', 'white', '-alpha',
+            'remove', '-alpha', 'off', tmppng.path
+
+          if File.size? tmppng.path
+            stdout, stderr, status = Open3.capture3 img2pdf, '--output',
+              pdf.path, tmppng.path
+          end
+
+          tmppng.unlink
+        end
+      end
+
       file.unlink
-      raise "Failed to convert #{self.name} to PDF" unless File.size? pdf.path
+
+      unless File.size? pdf.path
+        STDERR.print stderr unless stderr.empty?
+        raise "Failed to convert #{self.name} to PDF"
+      end
+
       return pdf
     end
 


Mime
View raw message