Coder Social home page Coder Social logo

Comments (3)

jondkinney avatar jondkinney commented on June 9, 2024

The only way I know of to get the signed version of the PDF would be to get it in the docusign_response action where we know the document has actually been signed. When you're in the embedded_signing action they have the ability to refuse to sign or decide to do it later, etc. The PDF you're generating in that action is explicitly the unsigned one, which is why you're seeing that when pulling it up. below is some code from the app I use inside the docusign_response action. Note, I'm storing the envelope_id in the db so I can reference it later. This would theoretically allow you to pull up the PDF at any other time, but I store it locally to limit API calls.

cf (campaign_firm) refers to a model association that links the two users signing the contract

# get the PDF and store it locally so both parties can reference it
fn = "#{SecureRandom.urlsafe_base64(7)}.pdf"
save_path = Rails.root.join("docusign_docs/#{fn}").to_s

client.get_document_from_envelope(
  envelope_id:     cf.envelope_id,
  document_id:     1,
  local_save_path: save_path
)

# Store the filename so we can reference it later. Note: I intentionally
# did not store the full file path in case we need to move where the
# files are stored
cf.update_attributes(pdf_name: fn)

from docusign_rest.

gbatta avatar gbatta commented on June 9, 2024

Thanks! Persisting the envelope_id in the database as a model attribute what I ended up doing in the meantime, though I wasn't sure if it was optimal, so glad to see you confirm it.

I also didn't have the pdf filename security that you included here; great idea, and I'll make sure to include that also.

from docusign_rest.

jondkinney avatar jondkinney commented on June 9, 2024

Using SecureRandom like that could still technically lead to file name conflicts (aka it's possible SecureRandom could generate the same name twice) and that would be bad. I have a module that I include in any controller that uses Docusign to deal with some of the repeated code. Feel free to use this as-is or pull out a piece of it if you like.

module PDFGenerator
  def generate_pdf_name(attrs)
    file_dir = attrs[:file_dir]
    file_name_prefix = attrs.fetch(:file_name_prefix, '')

    begin
      name = "#{file_name_prefix}#{SecureRandom.urlsafe_base64(7)}"
    end while File.exists?("#{file_dir}/#{name}.pdf")

    name
  end

  def generate_pdf(attrs)
    view_template    = attrs[:view_template]
    locals           = attrs[:locals]
    file_dir         = attrs[:file_dir]
    file_name_prefix = attrs.fetch(:file_name_prefix, '')

    html = render_to_string(
      partial: view_template,
      locals: locals,
      layout: false
    )

    kit = PDFKit.new(html)

    css_file_path = "#{Rails.root}/app/assets/stylesheets/pdf.css"
    kit.stylesheets << css_file_path if css_file_path

    FileUtils.mkdir_p(file_dir)

    fn = "#{generate_pdf_name(file_name_prefix: file_name_prefix,
                              file_dir: file_dir)}.pdf"
    fp = Rails.root.join("#{file_dir}/#{fn}").to_s

    kit.to_file(fp)

    {file_name: fn, file_path: fp}
  end
end

from docusign_rest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.