如何让PDFView显示PDF而无需调整窗口大小或进行滚动以强制重绘.
这就好像setNeedsDisplay需要完成,但这没有任何效果.
我有一个最简单的应用程序,我可以想到使用PDFView显示PDF.
基本上在Xcode中创建一个新的应用程序并将代码更改为:
// AppDelegate.h
#import <Cocoa/Cocoa.h>
#import <Quartz/Quartz.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet PDFView *pdfView;
@end
Run Code Online (Sandbox Code Playgroud)
和
// AppDelegate.m
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize pdfView = _pdfView;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSURL *pdfURL = [[NSBundle mainBundle] URLForResource:@"TestPage" withExtension:@"pdf"];
PDFDocument *document = [[PDFDocument alloc] initWithURL: pdfURL];
self.pdfView.document = document;
}
@end
Run Code Online (Sandbox Code Playgroud)
我注意到Apple代码示例PDFKitLinker2中的相同行为,其中必须滚动PDF或调整窗口大小以显示PDF.
当然,我是Cocoa开发世界的新手,但我搜索过并发现没有一个很小的例子来展示如何显示PDF.我所生产的内容似乎是正确的,但我怀疑甚至PDFKitLinker2似乎也不正确.
我已经创建了一个Xcode项目的Git仓库,它为那些有时间提供帮助和/或评论的人展示了这个问题.
git clone git@bitbucket.org:sotapme/pdfview_open.git …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用pdfkit将显示图像从facebook cdn转换为pdf的html页面.我使用rails 4.2,pdfkit 0.6.2和wkhtmltopdf-binary 0.9.9.3.
# Gemfile
gem 'pdfkit'
gem 'wkhtmltopdf-binary'
# controller
def generate_pdf
@booklet = Booklet.find params[:id]
@cover = Image.last
@images = @booklet.images.sort_by(&:uploaded_at)
respond_to do |format|
format.html
format.pdf do
html = render_to_string(layout: true , action: "generate_pdf.html.haml")
kit = PDFKit.new(html, page_size: 'A4', orientation: 'Landscape')
`sass vendor/assets/stylesheets/bootstrap.scss tmp/bootstrap.css`
`sass vendor/assets/stylesheets/custom.scss tmp/custom.css`
kit.stylesheets << "#{Rails.root}/tmp/bootstrap.css"
kit.stylesheets << "#{Rails.root}/tmp/custom.css"
pdf = kit.to_pdf
send_data pdf, filename: 'booklet.pdf', type: 'application/pdf'
end
end
end
# application.scss
@import 'bootstrap';
@import 'custom';
# config/application.rb
require 'pdfkit'
config.middleware.use …Run Code Online (Sandbox Code Playgroud) 我目前正在使用Flask. 我的主管要求一个功能应该是将当前页面保存为 PDF(用于打印)。为此,我正在使用pdfkit. 在有人提议使用之前weasyprint:我尝试安装它 2 小时无济于事,所以我放弃了。我正在使用Bootstrapcss 及其 JavaScript 使应用程序看起来更令人愉快。
我正在使用以下文件结构(如教程所建议的那样Flask)
+-- name_of_app
| +-- static
| +-- css
| +-- css files
| +-- js
| +-- javascript files
| +-- templates
| +-- .html templates
| +-- __init__.py
| +-- config.py
| +-- forms.py
| +-- views.py
+-- run.py
Run Code Online (Sandbox Code Playgroud)
为了尝试pdfkit一下,我每次生成页面时都会生成 PDF,即在views.py. 所有页面均基于以下模板
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="static\css\bootstrap.css" rel="stylesheet" media="screen">
<link href="static\css\bootstrap-theme.css" …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用pdfkit将网页转换为PDF,但它显示以下错误
Traceback (most recent call last):
File "<ipython-input-39-33289a2ef087>", line 1, in <module>
runfile('H:/Python/Practice/pdf_read_write.py', wdir='H:/Python/Practice')
File "C:\Program Files\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "C:\Program Files\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "H:/Python/Practice/pdf_read_write.py", line 10, in <module>
config = pdfkit.configuration(wkhtmltopdf="C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe")
File "C:\Program Files\Anaconda3\lib\site-packages\pdfkit\api.py", line 83, in configuration
return Configuration(**kwargs)
File "C:\Program Files\Anaconda3\lib\site-packages\pdfkit\configuration.py", line 27, in __init__
'https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf' % self.wkhtmltopdf)
OSError: No wkhtmltopdf executable found: "C:\Program Files\wkhtmltopdin\wkhtmltopdf.exe"
If this file exists please check that this process can …Run Code Online (Sandbox Code Playgroud) 我无法将我的.SVG文件转换为.PDF。我将.SVG代码嵌入html并尝试从字符串中加载pdf。除了嵌入的图像,其他所有内容都显示良好。他们根本不露面。这是图像的svg的示例...
<image xlink:href="data:image.png;base64, '...'" height="45", width="70", y="10", x="10" />
Run Code Online (Sandbox Code Playgroud)
其中的“ ...”用正确的base64字符串填充。我也亲自检查过base64代码。任何关于可能出问题的想法将不胜感激!
我可以在iOS11,PDFKit的PDFView的usePageViewController方法的withViewOpetions参数中指定哪些键/值?
我有一个奇怪的错误。我尝试使用 pdfkit 将 HTML 字符串转换为 PDF,但收到“没有这样的文件或目录:'which'”。我知道 pdfkit 使用 wkhtmltopdf,并且它尝试使用 which 来找到该实用程序。我不知道发生了什么事。'which' 工作正常,wkhtmltopdf 安装在 /usr/bin/ 上。我在这一行收到错误:
pdf = pdfkit.from_string(html, False, options=options)
Run Code Online (Sandbox Code Playgroud)
和回溯:
Traceback:
File "/srv/zboss-git/venv/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/srv/zboss-git/venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/srv/zboss-git/venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/srv/zboss-git/venv/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/srv/zboss-git/venv/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/srv/zboss-git/zboss/issues/views.py" in export_comments
453. pdf = pdfkit.from_string(html, False, options=options)
File "/srv/zboss-git/venv/lib/python3.5/site-packages/pdfkit/api.py" in …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Firebase 函数创建 PDF 文件并将其保存到Firebase 存储。我使用了在这个答案 中找到的代码。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const pdfkit = require('pdfkit');
admin.initializeApp(functions.config().firebase);
exports.createPDF = functions.https.onRequest((req, res) => {
const myPdfFile = admin.storage().bucket("gs://MYBUCKETNAME.appspot.com/").file('test.pdf');
const doc = new pdfkit();
const stream = doc.pipe(myPdfFile.createWriteStream());
doc.fontSize(25).text('Hello World!', 100, 100);
doc.end();
stream.on('finish', function () {
res.status(200).send({status: "ok"});
});
});
Run Code Online (Sandbox Code Playgroud)
问题:
代码正在运行并且文件已生成。问题是该文件的 MIME 类型为application/octet-stream。所以我无法下载或打开它。

如何保存 pdf 文件并指定 MIME 类型?或者创建后更改?
pdfkit node.js google-cloud-storage firebase google-cloud-functions
我尝试使用 pdfkit 从字符串生成 pdf 文件。但是,在结果中,所有换行符都\n将被忽略。因此,我得到了一个 pdf 文件,其中的字符串被打印为单个句子。例如:
import pdfkit
s = "Some text\n"
s += "Ok, let's add something more\n"
s += "And more..."
pdfkit.from_string(s,"file.pdf",{'encoding':'utf-8'})
Run Code Online (Sandbox Code Playgroud)
以下代码会产生以下输出(即包含以下文本的 pdf 文件):
一些文字 好的,让我们添加更多内容 还有更多...
然而,这就是我想要实现的目标:
一些文字
好的,让我们添加更多内容
和更多...
问题分为不同部分:
我在 Raspbian 上使用 Python 3.8 创建了一个简单的 Flask 1.1.1 应用程序。
我正在尝试使用PdfKit实现 PDF 文件的生成,但我在引导程序渲染方面遇到了一些问题。
我已经尝试在谷歌上搜索,我发现了两篇文章:
正如 repo README.md 中所说,我已经安装了 wkhtmltopdf 的静态二进制文件:
警告!debian/ubuntu repos 中的版本减少了功能(因为它在没有 wkhtmltopdf QT 补丁的情况下编译),例如添加大纲、页眉、页脚、TOC 等。要使用此选项,您应该从 wkhtmltopdf 站点安装静态二进制文件,或者您可以使用此脚本.
有一个选项被称为--user-style-sheet用作某事的解决方法:
警告 这是 wkhtmltopdf 中此错误的解决方法。您应该先尝试 --user-style-sheet 选项。
问题是我不知道是哪个错误,因为参考页面是离线的。
项目文件夹结构如下:
WebServer/
??? Rembe
? ??? __init__.py
? ??? static
? ? ??? content …Run Code Online (Sandbox Code Playgroud) pdfkit ×10
python ×5
pdf ×4
flask ×2
python-3.x ×2
wkhtmltopdf ×2
cocoa ×1
css ×1
firebase ×1
ios ×1
ios11 ×1
javascript ×1
node.js ×1
objective-c ×1
svg ×1
xcode ×1