gpt4 book ai didi

css - 如何使用 PDFKit 在 Rails 中捕获想要的 html div

转载 作者:太空宇宙 更新时间:2023-11-04 03:48:42 27 4
gpt4 key购买 nike

我想在我指定的区域生成 PDF(比如在 div id 标签中)我已经按照 railcast 的教程和文档开始使用 rails gem PDFKit。

所以一个简单的 .pdf 链接就成功了。

 <%= link_to 'Open PDF', invoice_path(@invoice, :format => "pdf"), :class => "btn btn-md btn-primary" %>

现在的问题是如何只捕获想要的区域?我在这里探索了 html 答案:Print <div id=printarea></div> only?

我试过类似的方法,但没有用。

@media print
{
body * { visibility: hidden; }
#printableArea * { visibility: visible; }
#printableArea { position: absolute; top: 40px; left: 30px; }
}

在 show.html.erb 中

<div id="printableArea">
something in PDF
</div>

我找到了一些其他资源,例如 http://www.sitepoint.com/pdf-generation-rails/但它只是要求放置标签来隐藏它。我的问题更多是隐藏其余部分并使我想要的 div 可见。

最佳答案

不确定这是否是最好的方法,但我在我不想在我的 pdf 中显示的所有代码周围放置了一个 if 语句,如下所示:

<% if params[:format] != "pdf" %>
All the code I don't want to show in my pdf
<% end %>

或者您可以将 pdf 内容与 html 内容完全分开

<% if params[:format] == "pdf" %>
All the code I want to show in my pdf only
<% else %>
All the code I want to show in html only
<% end %>

你可以使用不同的布局来不显示导航栏和页脚

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<%= wicked_pdf_stylesheet_link_tag 'application' %>
<link href="http://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

<%= wicked_pdf_javascript_include_tag "application" %>
<%= csrf_meta_tags %>
<%= favicon_link_tag 'https://s3.amazonaws.com/url/favicon.ico' %>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://js.braintreegateway.com/v1/braintree.js"></script>
</head>
<body id="body_tag">

<%= yield %>

</body>
</html>

Controller

respond_to do |format|
format.html
format.pdf do
render :pdf => "show",
template: 'deliveries/show.html.erb',
layout: 'pdf.html'
end
end

关于css - 如何使用 PDFKit 在 Rails 中捕获想要的 html div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23722183/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com