标签: pdfkit

使用rspec和pdfkit测试pdf的下载

我正在开发一个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.现在我想测试整个事情,包括:

  • 实例变量的分配(当然很简单,有效)
  • 发送数据,即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)

pdf rspec ruby-on-rails pdfkit wkhtmltopdf

9
推荐指数
2
解决办法
8087
查看次数

Rails 3.1资产管道与PDFKit

我正在使用带有rails 3.1的PDFkit.在过去,我能够使用render_to_string函数并从该字符串创建pdf.然后我按如下方式添加样式表.我的问题是我不知道如何从资产管道中访问它们.(这就是我在rails 3.0中的表现)

html_string = render_to_string(:template => "/faxes/show.html.erb", :layout => 'trade_request')
kit = PDFKit.new(html_string, :page_size => 'Letter')
kit.stylesheets << "#{Rails.root.to_s}/public/stylesheets/trade_request.css"
Run Code Online (Sandbox Code Playgroud)

所以我的问题是如何通过资产管道直接从我的控制器访问我的css文件?

我知道我可以使用带有PDFkit的Rack Middleware将pdf呈现给浏览器,但在这种情况下,我需要将pdf发送到第三方传真服务.

谢谢你的帮助.

瑞安

ruby-on-rails pdfkit asset-pipeline

8
推荐指数
1
解决办法
2889
查看次数

使用PDFKit导入完整的SVG

有谁知道是否可以将完整的SVG导入PDFKit文档?我可以从文档中看到它有完整的SVG支持,并且有绘制路径的方法等,但我看不到导入完整SVG文档的方法.

javascript svg pdfkit

8
推荐指数
2
解决办法
3819
查看次数

Python Django PDFKIT - [Errno 9]错误的文件描述符

我使用pdfkit和wkhtmltopdf生成pdf文档.当我生成第一个pdf时,一切都很好.当我快速(在5秒内)生成另一个我得到错误[Errno 9]错误的文件描述符.如果我关闭错误(退回浏览器)并再次打开,它将创建pdf.

我的views.py

config = pdfkit.configuration(wkhtmltopdf='C:/wkhtmltopdf/bin/wkhtmltopdf.exe')
pdfgen = pdfkit.from_url(url, printname, configuration=config)
pdf = open(printname, 'rb')

response = HttpResponse(pdf.read())
response['Content-Type'] = 'application/pdf'
response['Content-disposition'] = 'attachment ; filename =' + filename
pdf.close()
return response
Run Code Online (Sandbox Code Playgroud)

也许重要的说明:我在IIS8上运行这个站点,当从命令行(python manage.py runserver)运行时,错误不存在.

有关如何处理此错误的任何指导都会很棒.

python iis django pdfkit

8
推荐指数
1
解决办法
891
查看次数

支持 Unicode 的 JavaScript pdf 生成库

我想在客户端使用 JavaScript 生成一个 pdf 文件。首先我尝试使用 jsPdf API 。但它不适用于像中文这样的 Unicode 字符。

是否有任何选项可以增强 jspdf 以支持 Unicode 或任何其他支持 Unicode 的库。

Pdfmake API 说它支持 Unicode,但是当我尝试它也不起作用时,我在那里查看了放置 Unicode 字符的演示示例。

我尝试在 Node.js 中使用 pdfkit 但没有正确创建 pdf

var PDFDocument = require("pdfkit");
var fs = require('fs');

var doc = new PDFDocument;

doc.pipe(fs.createWriteStream('output.pdf'));

doc.fontSize(15);
doc.text('Generate PDF! ????????');

doc.end();
Run Code Online (Sandbox Code Playgroud)

在 pdf 中,它创建为生成 PDF!o"[Wo"[Wo"[Wo"[W

另外,我可以在 pdfMake 中添加多种字体吗?

 var fontDescriptors = {
    Roboto: {
      normal: 'examples/fonts/Roboto-Regular.ttf',
      bold: 'examples/fonts/Roboto-Medium.ttf',
      italics: 'examples/fonts/Roboto-Italic.ttf',
      bolditalics: 'examples/fonts/Roboto-Italic.ttf'
    }
  };

  var printer = new pdfMakePrinter(fontDescriptors);
Run Code Online (Sandbox Code Playgroud)

javascript pdf unicode pdfkit jspdf

8
推荐指数
1
解决办法
8402
查看次数

PDFKit - 使用pageViewController的PDFView - 滑动到下一页时页面渲染速度慢

我有一个设置为使用pageViewController的PDFView:

    let pdfView = PDFView()
    let pdfDoc = PDFDocument(url: Bundle.main.url(forResource: "test", withExtension: "pdf")!)
    pdfView.document = pdfDoc
    pdfView.autoScales = true
    pdfView.displayDirection = .horizontal
    pdfView.usePageViewController(true, withViewOptions: [:])
Run Code Online (Sandbox Code Playgroud)

每当我滑动到下一页时,下一页在模糊渲染之前会模糊半秒.这很烦人.我可以以某种方式预加载页面吗?我没有在文档中找到任何属性/方法.

(有趣的是,我有相同的问题PreviewMacOS时,它在全屏模式[永远以来,在所有Mac我自己])

它不依赖于pdf(文件)大小.每次尝试的pdf都会出现问题.

谢谢你的帮助

performance pdfkit ios swift

8
推荐指数
1
解决办法
2513
查看次数

使用 python 和 pdfkit 将 PDF 转换为 HTML

Adobe在此网站上写了有关使用 pdfkit 将 pdf 转换为 html 的内容

\n

他们使用pdfkit.from_pdf(...)方法。

\n
\n

此脚本使用 \xe2\x80\x98pdfkit\xe2\x80\x99 库将 PDF 文件转换为 HTML。要使用此脚本,您需要安装 \xe2\x80\x98pdfkit\xe2\x80\x99 库...

\n
\n

当我想使用这个方法时出现错误

\n
Traceback (most recent call last):\n  File "C:\\TestPdfToHtml\\script.py", line 7, in <module>\n    html_file = pdfkit.from_pdf(pdf_file, "my_html_file.html")\n                ^^^^^^^^^^^^^^^\nAttributeError: module \'pdfkit\' has no attribute \'from_pdf\'. Did you mean: \'from_url\'?\n
Run Code Online (Sandbox Code Playgroud)\n

我该如何解决这个问题?

\n

下面是完整的脚本

\n
import pdfkit\n# Read the PDF file\npdf_file = open(\'test2.pdf\', \'rb\')\n# Convert the PDF to HTML\nhtml_file = pdfkit.from_pdf(pdf_file, "my_html_file.html")\n# Close the PDF file\npdf_file.close()\n
Run Code Online (Sandbox Code Playgroud)\n

python pdf-to-html pdfkit

8
推荐指数
1
解决办法
2万
查看次数

pdfkit 使用 from_string 创建页面

我正在使用 pdfkit 从字符串生成 pdf 文件。

问:我传递给 pdfkit 的每个字符串我都希望它作为输出 pdf 中的一个新页面。

我知道使用如下 from_file 是可能的。但是,我不想将它写入文件并使用它。

pdfkit.from_file(['file1', 'file2'], 'output.pdf') 这将创建 2 页的 output.pdf 文件。

类似下面的东西可能吗?

pdfkit.from_string(['ABC', 'XYZ'], 'output.pdf') 
Run Code Online (Sandbox Code Playgroud)

它应该在 output.pdf 文件的第 1 页和第 2 页中写“ABC”和“XYZ”

python pdfkit python-pdfkit

7
推荐指数
1
解决办法
3117
查看次数

在Firebase云功能上使用PDFMake的承诺

我正在使用PDFMake(一种变体PDFKit)使用实时数据库触发器在Firebase云功能上生成PDF.该函数从数据库获取所有相关数据,然后将其传递给应该生成PDF的函数.

所有这些都是使用Promises完成的.一切正常,直到实际生成PDF.

这是我的主要事件监听器中的代码:

exports.handler = (admin, event, storage) => {
  const quotationData = event.data.val();
  // We must return a Promise when performing async tasks inside Functions
  // Eg: Writing to realtime db
  const companyId = event.params.companyId;
  settings.getCompanyProfile(admin, companyId)
  .then((profile) => {
    return quotPdfHelper.generatePDF(fonts, profile, quotationData, storage);
  })
  .then(() => {
    console.log('Generation Successful. Pass for email');
  })
  .catch((err) => {
    console.log(`Error: ${err}`);
  });
};
Run Code Online (Sandbox Code Playgroud)

要生成PDF,这是我的代码:

exports.generatePDF = (fonts, companyInfo, quotationData, storage) => {
  const printer = new PdfPrinter(fonts);
  const …
Run Code Online (Sandbox Code Playgroud)

pdfkit node.js promise firebase pdfmake

7
推荐指数
1
解决办法
1020
查看次数

在 UIViewControllerRepresentable 中传递数据

我正在尝试使用 UIKit 制作 PDFView 并从以前的 SwiftUI 视图传递数据。数据本身是一个带有文件名的字符串。不知何故,该字符串没有从 UIViewControllerRepresentable 传递到 UIViewController 并且保持为空。我实在找不到发生这种情况的原因。你能检查一下我哪里错了吗?我希望这段代码足够了。

我正在使用 NavigationLink(destination: FileViewerWrapper(file: "some string")) {}

import PDFKit
import UIKit
import SwiftUI

let pdfView = PDFView()

class FileViewer: UIViewController {

var file = String()


override func viewDidLoad() {
    super.viewDidLoad()

    print("\n\n\n-----------------------\(file)\n-------------------------------\n\n\n")

    pdfView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(pdfView)

    pdfView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
    pdfView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
    pdfView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
    pdfView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true

    guard let path = Bundle.main.url(forResource: file, withExtension: "pdf", subdirectory: "Files") else { return }

    if let document = PDFDocument(url: …
Run Code Online (Sandbox Code Playgroud)

xcode pdfkit swiftui

7
推荐指数
1
解决办法
3165
查看次数