Skip to content

Instantly share code, notes, and snippets.

@darkhelmet
Forked from bonyiii/gist:2135731
Created January 25, 2016 20:01
Show Gist options
  • Select an option

  • Save darkhelmet/1de2b726e87be1a7a671 to your computer and use it in GitHub Desktop.

Select an option

Save darkhelmet/1de2b726e87be1a7a671 to your computer and use it in GitHub Desktop.

Revisions

  1. @bonyiii bonyiii created this gist Mar 20, 2012.
    82 changes: 82 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    http://stackoverflow.com/questions/68569/text-watermark-on-website-how-to-do-it

    <style type="text/css">
    #watermark {
    color: #d0d0d0;
    font-size: 200pt;
    -webkit-transform: rotate(-45deg);
    -moz-transform: rotate(-45deg);
    position: absolute;
    width: 100%;
    height: 100%;
    margin: 0;
    z-index: -1;
    left:-100px;
    top:-200px;
    }
    </style>
    <div id="watermark">
    <p>This is the test version.</p>
    </div>


    http://stackoverflow.com/questions/5399458/render-to-string-does-not-find-partials-pdfkit-controller-response

    respond_to do |format|
    format.html
    format.pdf {
    html = render_to_string(:layout => false , :action => "constitution.pdf.haml")
    kit = PDFKit.new(html)
    kit.stylesheets << "#{Rails.root}/public/stylesheets/pdf.css"
    send_data(kit.to_pdf, :filename => "#{@organisation_name} Constitution.pdf",
    :type => 'application/pdf', :disposition => 'inline')
    return
    }
    end


    http://metaskills.net/2011/03/20/pdfkit-overview-and-advanced-usage/
    /* Page Breaks */
    .pb_before { page-break-before:always !important; }
    .pb_after { page-break-after:always !important; }
    .pbi_avoid { page-break-inside:avoid !important; }

    # My with authentication (sending session cookie name and value)

    kit = PDFKit.new(model_url(@model), 'margin-left' => '10mm', 'margin-right' => '10mm', page_size: 'A5', cookie: "#{Rails::Application.config.session_options[:key]} #{cookies[Rails::Application.config.session_options[:key]]}")
    file = kit.to_file("#{Rails.root}/tmp/invoices/#{@invoice.id}.pdf")

    # My way, rendering file

    # config/initializers/mime_types.rb
    Mime::Type.register 'application/pdf', :pdf

    #Controller

    def show
    @invoice = Model.find(params[:id])

    respond_to do |format|
    format.html { render layout: 'invoice' }
    format.pdf {

    unless File.exist?("#{Rails.root}/tmp/model/#{@model.id}.pdf")
    file = pdf_kit.to_file("#{Rails.root}/tmp/model/#{@model.id}.pdf")
    end

    send_file "#{Rails.root}/tmp/model/#{@model.id}_pre.pdf", :type => 'application/pdf'

    }
    format.xml { render :xml => @invoice }
    end
    end


    private

    def pdf_kit
    html = render_to_string(action: :show, layout: 'invoice')
    kit = PDFKit.new(html, 'margin-left' => '10mm', 'margin-right' => '10mm', page_size: 'A5',)
    kit.stylesheets << "#{Rails.root}/public/stylesheets/model_pdf.css"
    kit
    end