我从一个表格的 html页面生成pdf报告.
我正在使用wkhtmltopdf.
当生成pdf时,它会破坏tr标签中的任何位置.
我想避免它.
我正在使用ruby on rails(3.1)进行项目.我的要求是从html内容生成pdf.所以我使用pdfkit gem.
在某些页面中,单行中的字符在页面之间部分切割.当我使用pdfkit gem将html转换为pdf时
版本的wkhtmltopdf:wkhtmltopdf - 0.11.0 rc1
操作系统:Linux CentOS 5.5
在下图中显示页面之间部分切割的字符.
请提出解决方案.
例1

例2

在阅读之后,在我看来Prawn已经出局并且wkhtmltopdf已经进入.看起来Rails 的PDFKit和wicked_pdf宝石也是新的酷.所以我发现了Ryan关于如何使用PDFKit的截屏视频.我安装了所有东西,在CLI上测试wkhtmltopdf没有任何问题,摆弄Rails设置运行多个进程,所以资产管道工作,一切似乎都很好,除了我仍然停留在过程的最后(实际上得到PDF来自服务器的响应).
当我请求.pdf我的视图版本(我正在使用PDFKit中间件选项)时,我的浏览器就坐在那里等待响应,但是一旦我杀死了Rails进程,我希望得到的PDF只会在我的浏览器中弹出窗口.是什么赋予了?
wkhtmltopdf -V它时说0.10.0_rc2)config.middleware.use "PDFKit::Middleware"我的application.rb文件使用PDFKit中间件.gem 'pdfkit'在我的Gemfile中并与Bundler一起安装.pdf用我的mime_types.rb初始化程序设置mime-typeMime::Type.register_alias "application/pdf", :pdfconfig.threadsafe!到config/environments/development.rb多个线程,因此资产管道不与PDF引擎冲突wkhtmltopdf http://www.google.com google.pdf,它按预期生成了Google主页的PDF这是由Rails呈现的常规html页面(我已经模糊了客户端详细信息):

当我尝试导航到时,这是Rails的CLI输出localhost:3000/some/path.pdf.(应用程序在等待响应时挂起):

当我最终杀死Rails进程时ctrl-c,PDF终于出现在浏览器中,因为我期望看到它(CSS和HTML正确呈现,所以资产似乎加载正常):

为wicked_pdf交换PDFKit并获得相同的结果似乎让我觉得问题不在于那些库,而是与我的开发环境有关.但是wkhtmltopdf在命令行中运行良好,这让我觉得它和QT正在完成它们的工作.问题必须在Rails中.也许我没有正确配置?
如何确定问题到底是什么以及如何解决?
如果你能帮助我,我会爱你的<3
我也尝试过使用另一种方法来渲染PDF(with .to_pdf)而没有中间件选项,如下所示(这样做我config.middleware.use "PDFKit::Middleware"从我的 …
我尝试使用以下命令在 Windows 的 conda 中安装新包:
conda install -c conda-forge python-pdfkit
但出现以下错误:
收集包元数据 (current_repodata.json):已完成 解决环境:初始冻结解决失败。使用灵活的解决方案重试。解决环境:current_repodata.json 中的 repodata 失败,将使用下一个 repodata 源重试。
我尝试了以下解决方法,但没有用,仍然出现相同的错误:
解决方法1:
$conda create --name myenv
$conda activate myenv
Run Code Online (Sandbox Code Playgroud)
解决方法2:
conda config --set ssl_verify false
Run Code Online (Sandbox Code Playgroud) 我尝试在我的Windows 8机器上安装pdfkit Python API.我遇到了与路径有关的问题.
Traceback (most recent call last):
File "C:\Python27\pdfcre", line 13, in <module>
pdfkit.from_url('http://google.com', 'out.pdf')
File "C:\Python27\lib\site-packages\pdfkit\api.py", line 22, in from_url
configuration=configuration)
File "C:\Python27\lib\site-packages\pdfkit\pdfkit.py", line 38, in __init__
self.configuration = (Configuration() if configuration is None
File "C:\Python27\lib\site-packages\pdfkit\configuration.py", line 27, in __init__
'https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf' % self.wkhtmltopdf)
IOError: No wkhtmltopdf executable found: ""
If this file exists please check that this process can read it. Otherwise please install wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf
Run Code Online (Sandbox Code Playgroud)
是否有人在Windows机器上安装了Python PDFKIt?如何解决此错误.
我的示例代码:
import pdfkit
import os
config = pdfkit.configuration(wkhtmltopdf='C:\\Python27\\wkhtmltopdf\bin\\wkhtmltopdf.exe') …Run Code Online (Sandbox Code Playgroud) 我有一个使用pdfkit创建pdf文档的rails 3.1应用程序,除了生成的pdf没有任何样式这一事实外,一切都按指定的方式工作.我假设wkhtmltopdf无法访问我的样式表,并且它不是一个更大的问题.有人会知道如何允许访问这些样式表吗?我基本上已经关注了这个主题的railscast#220,但是我必须创建一个新的初始化程序来使pdfkit与rails 3.1一起使用.
这是我必须使用的初始化程序,以使pdfkit与rails 3.1一起使用
ActionController::Base.asset_host = Proc.new { |source, request|
if request.env["REQUEST_PATH"].include? ".pdf"
"file://#{Rails.root.join('public')}"
else
"#{request.protocol}#{request.host_with_port}"
end
}
Run Code Online (Sandbox Code Playgroud)
pdf的链接如下所示:
<%= link_to 'Download PDF', load_path(@load, :format => "pdf") %>
Run Code Online (Sandbox Code Playgroud)
这将为我提供一个没有样式的pdf链接.
在我的application.rb中,我已经配置了pdfkit:
config.middleware.use PDFKit::Middleware, :print_media_type => true
Run Code Online (Sandbox Code Playgroud)
我还将此添加到我的layouts/application.html.erb文件中:
<%= stylesheet_link_tag "application", :media => "all" %>
Run Code Online (Sandbox Code Playgroud) 我正在使用姜戈。这是代码在views.py中。
def download_as_pdf_view(request, doc_type, pk):
import pdfkit
file_name = 'invoice.pdf'
pdf_path = os.path.join(settings.BASE_DIR, 'static', 'pdf', file_name)
template = get_template("paypal/card_invoice_detail.html")
_html = template.render({})
pdfkit.from_string(_html, pdf_path)
return FileResponse(open(pdf_path, 'rb'), filename=file_name, content_type='application/pdf')
Run Code Online (Sandbox Code Playgroud)
回溯如下。
[2022-09-05 00:56:35,785] ERROR [django.request.log_response:224] Internal Server Error: /paypal/download_pdf/card_invoice/MTE0Nm1vamlva29zaGkz/
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/opt/project/app/paypal/views.py", line 473, in download_as_pdf_view
pdfkit.from_string(str(_html), pdf_path)
File "/usr/local/lib/python3.8/site-packages/pdfkit/api.py", line 75, in from_string
return r.to_pdf(output_path)
File …Run Code Online (Sandbox Code Playgroud) 在我的应用程序目录中(在Windows上)我运行:
sudo pdfkit --install-wkhtmltopdf
Run Code Online (Sandbox Code Playgroud)
正如这里解释的那样,但我收到了这个错误:
'sudo'不被识别为内部或外部命令,可操作程序或批处理文件.
可能是什么问题呢 ?
长话短说:
更长的版本:
在 NodeJS 服务器中,我们需要根据请求参数将 HTML 字符串转换为 PDF 或 PNG。我们正在使用 puppeteer 生成部署在谷歌云功能中的 PDF 和 PNG(屏幕截图)。在我本地的 Docker 中运行此应用程序并将内存使用量限制为 100MB,这似乎有效。但是在云函数中,当我们将云函数设置为250MB内存时,它会抛出内存限制异常。作为临时解决方案,我们将云功能升级到 1 GB。
我们希望在没有任何无头浏览器方法的情况下尝试 puppeteer 的任何替代方案。另一个库 PDF-Kit 看起来不错,但它有 canvas api 类型的输入。我们不能直接提供 html。
对此有任何想法或意见
我正在尝试在我的prod服务器上使用wicked_pdf,但它仍然令人心烦:
RuntimeError (Failed to execute:
"/usr/bin/wkhtmltopdf" -q "file:////tmp/wicked_pdf20130709-23109-1adqx5g.html" "/tmp/wicked_pdf_generated_file20130709-23109-1ic5dbe.pdf"
Error: PDF could not be generated!
Command Error: wkhtmltopdf: cannot connect to X server
):
app/controllers/contrats_controller.rb:15:in `block (2 levels) in show'
app/controllers/contrats_controller.rb:11:in `show'
Run Code Online (Sandbox Code Playgroud)
我试着按照这个答案:wkhtmltopdf:无法连接到X服务器,但它仍然无法正常工作.
pdfkit ×10
wkhtmltopdf ×6
python ×3
html ×2
anaconda ×1
api ×1
css ×1
html-pdf ×1
node.js ×1
pdf ×1
puppeteer ×1
python-2.7 ×1
python-3.x ×1
ruby ×1
sudo ×1
wicked-pdf ×1