回形针和RMagick - PDF 的 3 页缩略图和重命名

发布于 2024-11-27 17:37:20 字数 1662 浏览 3 评论 0原文

我想上传 pdf 文件,并创建(作为单独的文件)一个缩略图,其中 pdf 的前 3 页水平对齐。 我设法用 RMagick 做了一个回形针处理器来生成该文件,但问题是:我希望单独的文件(具有缩略图样式的文件)具有正确的扩展名(例如 jpg)而不是原始的 pdf。 如果我仍然可以通过使用带样式的 url 方法获得正确的路径,例如:

>> attachment.url
=> "/some/path/id/original/test.pdf" # original file
>> attachment.url(:pdf_thumbnail)
=> "/some/path/id/pdf_thumbnail/test.jpg" # jpg file, not pdf

一些代码:

处理器

module Paperclip
  class PdfThumbnail < 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

模型(摘录)

has_attached_file :file, :styles => {:pdf_thumbnail => ""}, :processors => [:pdf_thumbnail]

它最终会做:

$ tree .
.
`-- 46
    |-- original
    |   `-- test.pdf
    `-- pdf_thumbnail
        `-- test.pdf

和:

$ 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

所以文件很好,但我想要一个不同的扩展名对于 pdf_thumbnail 样式。

有什么帮助吗?或者也许是另一种方式/更干净的代码?

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.:

>> attachment.url
=> "/some/path/id/original/test.pdf" # original file
>> attachment.url(:pdf_thumbnail)
=> "/some/path/id/pdf_thumbnail/test.jpg" # jpg file, not pdf

Some code:

Processor

module Paperclip
  class PdfThumbnail < 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

Model (extract)

has_attached_file :file, :styles => {:pdf_thumbnail => ""}, :processors => [:pdf_thumbnail]

It ends in doing:

$ tree .
.
`-- 46
    |-- original
    |   `-- test.pdf
    `-- pdf_thumbnail
        `-- test.pdf

and:

$ 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

So files are good, but I want a different extension for pdf_thumbnail style.

Any help? Or maybe another way/cleaner code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十雾 2024-12-04 17:37:20

我没有对此进行测试,但是怎么样:

has_attached_file :file, :styles => { :pdf_thumbnail => ["", :jpg] } ...

根据 Paperclip 文档,数组中的第二项应该强制使用该格式,尽管它没有指定它是否也适用于自定义处理器。但值得一试。

I didn't test this, but how about:

has_attached_file :file, :styles => { :pdf_thumbnail => ["", :jpg] } ...

According to the Paperclip docs the second item in the array should force the format, although it does not specify if it works with custom processors also. But worth a shot.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文