title: Wicked PDF, generating PDF from database table- images and style issues
body: <p>I have an uploader (internal use only) that will upload an HTML document to a binary column of a table in my client-facing website.  The client facing site has an index that allows the user to view the page as a normal website (using <code>send_data h_t.html_code, :type =&gt; "html", :disposition =&gt; "inline"</code>).  I also want to give the user the ability to download a PDF of the page.  For that I'm using wicked_pdf.</p>
  
  <p>The entire problem seems to stem from the fact that the data is stored in the database.  As strange as it sounds, it is vital to business operations that I get formatting exact.  The issue is I can't see any image, and the stylesheets/style tags don't have any effect.</p>
  
  <p>What I've tried-</p>
  
  <p>Gsub-</p>
  
  <pre><code>def show
    html = HtmlTranscript.find(params[:id])
    html_code = html.html_code.gsub('&lt;img src="/images/bwTranscriptLogo.gif" alt="Logo"&gt;','&lt;%= wicked_pdf_image_tag "bwTranscriptLogo.gif" %&gt;')
    html_code = html_code.gsub('&lt;link rel="StyleSheet" href="" type="text/css"&gt;','&lt;%= wicked_pdf_stylesheet_link_tag "transcripts.css" %&gt;')
    transcript = WickedPdf.new.pdf_from_string(html_code)
  
    respond_to do |format|
      format.html do
        send_data transcript, :type =&gt; "pdf", :disposition =&gt; "attachment"
      end
  ##### i never could get this part figured out, so if you have a fix for this...
  #      format.pdf do
  #      render :pdf =&gt; "transcript_for_#{@html.created_at}", :template =&gt; "html_transcripts/show.html.erb", :layout =&gt; false
  #      end
      end
    end
  </code></pre>
  
  <p>Using a template-</p>
  
  <pre><code>#Controller (above, modified)
  html = HtmlTranscript.find(params[:id])
  @html_code = html.html_code.gsub('&lt;img src="/images/bwTranscriptLogo.gif" alt="Logo"&gt;','&lt;%= wicked_pdf_image_tag "bwTranscriptLogo.gif" %&gt;')
  @html_code = @html_code.gsub('&lt;link rel="StyleSheet" href="" type="text/css"&gt;','&lt;%= wicked_pdf_stylesheet_link_tag "transcripts.css" %&gt;')
  transcript = WickedPdf.new.pdf_from_string(render_to_string(:template =&gt; "html_transcripts/show.html.erb", :layout =&gt; false))
  
  
  #view
  &lt;!-- tried with stylesheet &amp; image link tags, with wicked_pdf stylesheet &amp; image link tags, with html style &amp; img tags, etc --&gt;
  &lt;%= raw(@html_code) %&gt;
  </code></pre>
  
  <p>And both will generate a transcript- but neither will have style OR image.</p>
  
  <p>Creating an initializer-</p>
  
  <pre><code>module WickedPdfHelper
    def wicked_pdf_stylesheet_link_tag(*sources)
      sources.collect { |source|
        "&lt;style type='text/css'&gt;#{Rails.application.assets.find_asset("#{source}.css")}&lt;/style&gt;"
      }.join("\n").gsub(/url\(['"](.+)['"]\)(.+)/,%[url("#{wicked_pdf_image_location("\\1")}")\\2]).html_safe
    end
  
    def wicked_pdf_image_tag(img, options={})
      image_tag wicked_pdf_image_location(img), options
    end
  
    def wicked_pdf_image_location(img)
      "file://#{Rails.root.join('app', 'assets', 'images', img)}"
    end
  
    def wicked_pdf_javascript_src_tag(source)
      "&lt;script type='text/javascript'&gt;#{Rails.application.assets.find_asset("#{source}.js").body}&lt;/script&gt;"
    end
  
    def wicked_pdf_javascript_include_tag(*sources)
      sources.collect{ |source| wicked_pdf_javascript_src_tag(source) }.join("\n").html_safe
    end
  end
  </code></pre>
  
  <p>did absolutely nothing, and I have no idea what to try next.</p>
  
  <p>As a side note, the code to view the HTML version of the transcript is as follows:</p>
  
  <pre><code>def transcript_data
    h_t = HtmlTranscript.find(params[:id])
    send_data h_t.html_code, :type =&gt; "html", :disposition =&gt; "inline"
  end
  </code></pre>
  
  <p>It requires no view, as the html data is stored in the database, but I get image, style, etc.  Everything works with the HTML version- just not the PDF.</p>
  
  <p>I'm on ruby 1.8.7 with rails 3.0.20.</p>