senny / sablon

Ruby Document Template Processor based on docx templates and Mail Merge fields.
MIT License
446 stars 128 forks source link

Setting filename in Sablon.content not working? #140

Closed mroliveira7 closed 5 years ago

mroliveira7 commented 5 years ago

Hello, i'm testing this gem to check whether i'm gonna add it to a project i'm working on but i'm having some issues with images. The filename does not contain the extension of the file so i added the filename on the Sablon.content method. I'm on Rails 6.0 and ruby 2.6.3

Here is the code:

def upload_template
    file = params[:file]
    template = Sablon.template(file)
    request = Request.find_by id: 142
    image = request.files.first.blob

    image = Sablon.content(:image, ActiveStorage::Blob.service.send(:path_for, image.key), filename: 'teste.png')

    context = {
      current_time: "12/12/2019",
      title: 'Resume',
      request_type: "service",
      consultant_name: "Tester Backer",
      produto: '0101002',
      enterprise_name: 'Enteprise Name',
      cnpj_da_empresa: '00.000.000/0000-00',
      texto: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse hendrerit est augue, sit amet gravida dolor volutpat ut. Vestibulum lacinia nibh orci, id interdum ex dictum nec.",
      figure: image
    }

    template.render_to_file File.expand_path("vendor/teste.docx"), context
    send_file("#{Rails.root}/vendor/teste.docx", filename: "teste.docx")
end

I'm receiving an error Filename: '9o2a0zxg5cvgoxj34g047ytjwits' has no discernable extension

It looks like the filename that i added when calling Sablo.content is not working Did i miss something?

stadelmanma commented 5 years ago

@mroliveira7 I'm not sure why it isn't working. My assumption is that the ActiveStorage::Blob instance has an extract-able file path that is overriding the keyword arg.

You might try storing the raw image data in a different object, the copied code below is from the test suite and may work if adapted.

    #
    im_data = StringIO.new(IO.binread(@image_fixtures.join('clone.jpg')))
    trooper = Sablon.content(:image, im_data, filename: 'clone.jpg', properties: {height: '1cm', width: '4cm'})
mroliveira7 commented 5 years ago

Ok, i'll try that. Thanks! :+1:

stadelmanma commented 5 years ago

Closing issue, assuming the suggested fix above worked.