我正在使用Rails pdfkit gem来呈现多页pdf文件.渲染的pdf文件按预期获取CSS(SCSS)样式和分页符.但是,当我尝试在生产中渲染相同的pdf文档时,似乎样式只加载一些CSS规则并忽略其他如父容器width和height声明.这是我的父容器元素的CSS(SCSS):
.policy_pdf{
font-family: Arial, sans-serif;
.pdf-page{
width:98%;
height:17.1in;
margin:auto;
page-break-after:always;
...
@media screen{
border: 1px dotted red;
}
page-break-after:always;
}
...
}
Run Code Online (Sandbox Code Playgroud)
和PDFKit初始化程序:
PDFKit.configure do |config|
config.default_options = {
:page_size => 'Legal',
}
end
Run Code Online (Sandbox Code Playgroud)
doc周围的红线是我引入的CSS规则,用于显示页面边缘在生产中的呈现方式.
开发和生产(Digital Ocean Droplet)都使用相同版本的Ubutnu(16.04).
起初我认为我正在使用的一些CSS类,pdf-kit例如.page在编译时被一些冲突规则覆盖,所以我尝试使用独特的类名,.pdf-page而不是.page.
然后我试着看看它是否与SCSS编译有关.但是同一样式表中的嵌套边框和背景颜色声明会被"拾取"并呈现得很好.policy-pdf编译中的块application.css看起来也是正确的.
禁用smart-shrinking使PDF看起来更加"崩溃".
将大小/宽度CSS规则(内联和外部样式表)应用于html标记,如本文所示:
生产和开发都运行相同版本wkhtmltopdf的(~> 0.12.2 …
Ruby 1.8.7,Rails 3.0.4,PDFKit 0.5.0
我正在尝试使用PDFKit创建PDF而不使用中间件,因此我可以禁用javascript(那里有一个手风琴动作,隐藏了大量应该在PDF上的信息).但是,每当我尝试时,它都会失败,因为它表示我的视图(show.html.erb)中的部分缺失:
使用{:locale => [:en,:en],:formats => [:pdf],:handlers => [:erb,:rjs,:builder,:rhtml,:rxml]}缺少部分程序/详细信息
如果我删除对partials的引用,它工作正常.我也尝试将部分与show.html.erb放在同一目录中无济于事.这是我的控制器的show动作中的代码:
respond_to do |format|
format.html # show.html.erb
format.pdf {
html = render_to_string(:template => "show.html.erb")
kit = PDFKit.new(html, :disable_javascript => true )
send_data(kit.to_pdf, :filename => "test_pdf", :type => "application/pdf", :disposition => 'attachment')
}
end
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点,并保持部分?
编辑:现在我已经这样做了:
# config/initializers/pdfkit.rb
PDFKit.configure do |config|
config.default_options = {
:page_size => 'Legal',
:print_media_type => true,
:disable_javascript => true
}
end
Run Code Online (Sandbox Code Playgroud)
这样做的缺点是为我生成的每个PDF都关闭了javascript,但它现在就可以了.关于使部分工作仍然使用render_to_string的原始问题的任何答案仍然受到赞赏.
我试图在我的Rails应用程序中使用PDFKit从html生成的pdf中使用自定义字体.我在... app/assets/fonts /中添加了字体,并将其包含在我的html模板中:
css:
@font-face {
font-family: 'journal';
src: url(file://#{Rails.root.join('app', 'assets', 'fonts', 'journal.eot')});
src: url(file://#{Rails.root.join('app', 'assets', 'fonts', 'journal.woff')} format("woff")),
url(file://#{Rails.root.join('app', 'assets', 'fonts', 'journal.ttf')}) format("truetype");
}
Run Code Online (Sandbox Code Playgroud)
在css中称它为:
h1 {font-family: journal; }
Run Code Online (Sandbox Code Playgroud) 我从 PDFKit 收到 PDFView 的奇怪错误消息。在某些情况下我得到
不支持非隔离组。改变 ULBEWJ+MinionPro-Regular 中的字形 1(代码 32)。
在另一个实例中,我收到消息:.notdef:无映射。
在我显示的大多数 PDF 中,我没有收到任何错误消息。所有 PDF 的相同之处在于它们都在模拟器中正确显示。这和UTF有关系吗?知道如何修复这些错误消息吗?
我正在尝试使用PDFKit作为rails 3应用程序中的中间件.
我可以从命令行使用wkhtmltopdf就好了,但是我的应用程序一直让我犯这个错误
command failed: "/Users/bobby/.rvm/gems/ruby-1.9.2-p0/bin/wkhtmltopdf" "--page-size" "Letter" "--margin-top" "0.75in" "--margin-right" "0.75in" "--margin-bottom" "0.75in" "--margin-left" "0.75in" "--encoding" "UTF-8" "--print-media-type" "--quiet" "-" "-"
Run Code Online (Sandbox Code Playgroud)
如果我在终端运行它,它等待我的输入,所以我键入一些HTML,然后按Ctrl-d,它吐出似乎是一些PDF ...但没有运气轨道.
这就是我所拥有的:
application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
require 'pdfkit'
Bundler.require(:default, Rails.env) if defined?(Bundler)
module Mpr
class Application < Rails::Application
YEARS_ARRAY = (2006..2012).map {|y| [y,y]}.unshift(["Year",nil])
MONTHS_ARRAY = (1..12).map{|m| [ Date::MONTHNAMES[m], m]}.unshift(["All months",nil])
config.middleware.use "PDFKit::Middleware", :print_media_type => true
PDFKit.configure do |config|
config.wkhtmltopdf = '/Users/bobby/.rvm/gems/ruby-1.9.2-p0/bin/wkhtmltopdf'
end
end
end
Run Code Online (Sandbox Code Playgroud)
在我的控制器(第一行)
respond_to :html, :pdf
Run Code Online (Sandbox Code Playgroud)
我想我已经浏览了SO,Github和谷歌的所有主题,但没有运气.
任何人都可以帮助或指出我正确的方向吗?
谢谢,P
我在我的节点服务器上使用pdfkit(https://github.com/devongovett/pdfkit),通常创建pdf文件,然后将它们上传到s3.问题是pdfkit示例将pdf文档管道传输到节点写入流,该文件将文件写入磁盘,我按照示例操作并正常工作,但我现在的要求是将pdf文档管道传输到内存流而不是保存它在磁盘上(我还是上传到s3).我已经遵循了一些节点内存流程序,但它们似乎都没有与我一起使用pdf管道,我只能将字符串写入内存流.所以我的问题是:如何将pdf套件输出传输到内存流(或类似的东西),然后将其作为上传到s3的对象读取?
var fsStream = fs.createWriteStream(outputPath + fileName);
doc.pipe(fsStream);Run Code Online (Sandbox Code Playgroud)
提前致谢.
将注释添加到PDFPage(并在屏幕上呈现)后,无法在PDFPage/ 上更新其外观PDFView.
重现问题:
创建PDFAnnotation并将其添加到PDFPage:
let bounds = CGRect(x: 20.0, y: 20.0, width: 200.0, height: 200.0)
let annotation = PDFAnnotation(bounds: b, forType: .widget, withProperties: nil)
annotation.widgetFieldType = .text
annotation.backgroundColor = .gray
annotation.font = .systemFont(ofSize: 18)
annotation.widgetStringValue = "Test!"
page.addAnnotation(annotation)
Run Code Online (Sandbox Code Playgroud)在PDFPage上显示后,尝试编辑其边界/颜色/背景颜色/字符串值等:
annotation?.setValue("Help! SOS! Mayday!", forAnnotationKey: .widgetValue)
annotation?.color = .green
annotation?.bounds = CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0)
Run Code Online (Sandbox Code Playgroud)什么都没发生.
我知道一个诀窍:
page.removeAnnotation(annotation)
page.addAnnotation(annotation)
Run Code Online (Sandbox Code Playgroud)
但这是一种解决方法,而不是真正的解决方案.
我需要实现使用多页文本创建pdf的功能。
class PDFCreator {
func prepareData() -> Data {
//1
let pdfMetaData = [
kCGPDFContextCreator: "PDF Creator",
kCGPDFContextAuthor: "Pratik Sodha",
kCGPDFContextTitle: "My PDF"
]
//2
let format = UIGraphicsPDFRendererFormat()
format.documentInfo = pdfMetaData as [String: Any]
//3
let pageWidth = 8.5 * 72.0
let pageHeight = 11 * 72.0
let pageRect = CGRect(x: 0, y: 0, width: pageWidth, height: pageHeight)
//4
let renderer = UIGraphicsPDFRenderer(bounds: pageRect, format: format)
//5
let data = renderer.pdfData { (context) in
//6
context.beginPage()
self.addText("Lorem Ipsum is …Run Code Online (Sandbox Code Playgroud) 我正在开发一个rails 3.2应用程序,用户可以使用它下载pdfs.我很喜欢使用rspec和shoulda匹配器进行测试驱动开发,但是我对此感到茫然.
我的控制器里面有以下代码:
def show_as_pdf
@client = Client.find(params[:client_id])
@invoice = @client.invoices.find(params[:id])
PDFKit.configure do |config|
config.default_options = {
:footer_font_size => "6",
:encoding => "UTF-8",
:margin_top=>"1in",
:margin_right=>"1in",
:margin_bottom=>"1in",
:margin_left=>"1in"
}
end
pdf = PDFKit.new(render_to_string "invoices/pdf", layout: false)
invoice_stylesheet_path = File.expand_path(File.dirname(__FILE__) + "/../assets/stylesheets/pdfs/invoices.css.scss")
bootstrap_path = File.expand_path(File.dirname(__FILE__) + "../../../vendor/assets/stylesheets/bootstrap.min.css")
pdf.stylesheets << invoice_stylesheet_path
pdf.stylesheets << bootstrap_path
send_data pdf.to_pdf, filename: "#{@invoice.created_at.strftime("%Y-%m-%d")}_#{@client.name.gsub(" ", "_")}_#{@client.company.gsub(" ", "_")}_#{@invoice.number.gsub(" ", "_")}", type: "application/pdf"
return true
end
Run Code Online (Sandbox Code Playgroud)
这是相当简单的代码,它所做的就是配置我的PDFKit并下载生成的pdf.现在我想测试整个事情,包括:
我尝试过以下方法:
controller.should_receive(:send_data)
Run Code Online (Sandbox Code Playgroud)
但这给了我
Failure/Error: controller.should_receive(:send_data)
(#<InvoicesController:0x007fd96fa3e580>).send_data(any args)
expected: 1 time
received: …Run Code Online (Sandbox Code Playgroud) pdfkit ×10
pdf ×3
swift ×3
wkhtmltopdf ×3
ios ×2
pdfview ×2
css ×1
custom-font ×1
fonts ×1
ios11 ×1
memorystream ×1
node.js ×1
rspec ×1