How to upload to a bucket using Shrine and Prawn Pdf

This took me a little while to work out.

The Key

  • Render the prawn pdf, and make sure it’s done within a StringIO object.
  • Then you can attach away as normal:
    # add in your background worker
    pdf =   Prawn::Document.new
    pdf_string = StringIO.new(pdf.render) # The first part of the puzzle
    conversation = Conversation.new
    conversation.pdf = pdf_string  # this is the key. attach the pdf
    conversation.save

    # conversation.rb
    include FileUploader::Attachment(:pdf)

    # file_uploader.rb

    require "shrine/storage/memory"
    require "shrine/storage/google_cloud_storage"
    require "content_disposition"

    class FileUploader < Shrine
        plugin :determine_mime_type, analyzer: :marcel
    end
Written on June 4, 2020