Note that there are some explanatory texts on larger screens.

plurals
  1. POPaperclip & RMagick - 3-page thumbnail of PDF and renaming
    primarykey
    data
    text
    <p>I want to upload pdf file, and create (as a separate file) a thumbnail image with first 3 pages of the pdf aligned horizontally. I managed to do a Paperclip Processor with RMagick to generate that file, but the problem is: I want the seperate file (the one with style for thumbnail) have the right extension (ex. jpg) not original pdf. It would be great if I could still get the correct path by using the url method with style, for ex.:</p> <pre><code>&gt;&gt; attachment.url =&gt; "/some/path/id/original/test.pdf" # original file &gt;&gt; attachment.url(:pdf_thumbnail) =&gt; "/some/path/id/pdf_thumbnail/test.jpg" # jpg file, not pdf </code></pre> <p>Some code:</p> <h2>Processor</h2> <pre><code>module Paperclip class PdfThumbnail &lt; Processor def initialize(file, options = {}, attachment = nil) super @file = file @instance = options[:instance] @current_format = File.extname(@file.path) @basename = File.basename(@file.path, @current_format) end def make dst = Tempfile.new([@basename, 'jpg'].compact.join(".")) dst.binmode pdf = ::Magick::ImageList.new(File.expand_path(@file.path)) image = pdf[0..2].append(false) image.format = 'JPG' image.write(File.expand_path(dst.path)) dst.flush return dst end end end </code></pre> <h2>Model (extract)</h2> <pre><code>has_attached_file :file, :styles =&gt; {:pdf_thumbnail =&gt; ""}, :processors =&gt; [:pdf_thumbnail] </code></pre> <p>It ends in doing:</p> <pre><code>$ tree . . `-- 46 |-- original | `-- test.pdf `-- pdf_thumbnail `-- test.pdf </code></pre> <p>and:</p> <pre><code>$ file 46/original/test.pdf 46/original/test.pdf: PDF document, version 1.4 $ file 46/pdf_thumbnail/test.pdf 46/pdf_thumbnail/test.pdf: JPEG image data, JFIF standard 1.01 </code></pre> <p>So files are good, but I want a different extension for <code>pdf_thumbnail</code> style.</p> <p>Any help? Or maybe another way/cleaner code?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload