ruby on rails - How to handle a file_as_string (generated by Prawn) so that it is accepted by Carrierwave? -
i'm using prawn generate pdf controller of rails app,
... respond_to |format| format.pdf pdf = generatereportpdf.new(@object, view_context) send_data pdf.render, filename: "report", type: "application/pdf", disposition: "inline" end end
this works fine, want move generatereportpdf background task, , pass resulting object carrierwave upload directly s3.
the worker looks this
def perform pdf = generatereportpdf.new(@object) filestring = ??????? document = document.new( object_id: @object.id, file: filestring ) # file field used carrierwave end
how handle object returned prawn (?????
) ensure format can read carrierwave.
filestring = pdf.render_file 'filename'
writes object root directory of app. i'm on heroku not possible.
file = pdf.render
returns argumenterror: string contains null byte
filestring = stringio.new( pdf.render_file 'filename' )
returns typeerror: no implicit conversion of nil string
filestring = stringio.new( pdf.render )
returns activerecord::recordinvalid: validation failed: file not allowed upload nil files, allowed types: jpg, jpeg, gif, png, pdf, doc, docx, xls, xlsx
filestring = file.open( pdf.render )
returns argumenterror: string contains null byte
....and on.
what missing? stringio.new( pdf.render )
seems should work, i'm unclear why generating error.
it turns out stringio.new( pdf.render )
should indeed work.
the problem having filename being set incorrectly and, despite following advise below on carrierwave's wiki, bug elsewhere in code meant filename returning empty string. i'd overlooked assumed else needed
https://github.com/carrierwaveuploader/carrierwave/wiki/how-to:-upload-from-a-string-in-rails-3
my code ended looking this
def perform s = stringio.new(pdf.render) def s.original_filename; "my file name"; end document = document.new( object_id: @object.id ) document.file = s document.save! end
Comments
Post a Comment