开发者

How can I add page numbers to PDFKit generated PDFs?

I have multiple pages generated using PD开发者_StackOverflowFKit. How can I add page numbers to the bottom?


PDFKit.configure do |config|
  config.default_options = {
    header_right: "Page [page] of [toPage]"
  }
end

kit = PDFKit.new(body_html)

Read all detailed documentation here:
http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf-0.9.9-doc.html

PDFKit is just a wrap up for wkhtmltopdf application that is written in C.


you need to specify a footer like this:

kit = PDFKit.new(your_html_content_for_pdf, :footer_html => "#{path_to_a_footer_html_file}")

then in the footer file have this:

<html>
  <head>
    <script type="text/javascript">
      function subst() {
        var vars={};
        var x=document.location.search.substring(1).split('&');
        for(var i in x) {var z=x[i].split('=',2);vars[z[0]] = unescape(z[1]);}
        var x=['frompage','topage','page','webpage','section','subsection','subsubsection'];
        for(var i in x) {
          var y = document.getElementsByClassName(x[i]);
          for(var j=0; j<y.length; ++j) y[j].textContent = vars[x[i]];
        }
      }
    </script>
  </head>
  <body style="margin: 0;" onload="subst();">
    Page <span class="page"></span> of <span class="topage"></span>
  </body>
</html>

elements of classes 'frompage','topage','page','webpage','section','subsection','subsubsection' will get substituted with the appropriate data


I did page number with PDFKit, just by adding this:

%meta{:name => 'pdfkit-footer_right', :content => "[page]"}

in my haml file, in my RoR project.


For some weird reason, ( perhaps because I'm using slim ) - I have to use single quotes around the content, instead of double quotes - or else it attempts to escape the brackets and raw text "[page]" shows up, so try single quotes if you run into this issue with your pages.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜