我使用具有子类型墨迹的 PDFAnnotation() 类向 PDFDocument 添加墨迹注释。这个想法是捕获使用触摸绘制的签名。
受 UberSignature 的启发,我的 UIBezierPath 是一系列应该填充颜色的矩形。但是,当我将注释添加到 PDFDocument 时,它没有被填充。
当添加到 PDFAnnotation 时,UIBezierPath 的 fill() 方法似乎根本不执行任何操作?
如果我使用相同的 UIBezierPath 并将其绘制在 UIImage 上,它将正确填充纯色。
关于可能出什么问题的任何想法吗?
有问题的代码:
UIColor.red.setStroke()
UIColor.red.setFill()
var path = UIBezierPath()
path.append(myRectangles)
path.fill()
var annotation = PDFAnnotation(bounds: path.bounds, forType: .ink, withProperties: nil)
annotation.add(path)
myPDFPage.addAnnotation(annotation)
Run Code Online (Sandbox Code Playgroud)
在屏幕截图中,我尝试编写普通文本和两个示例行。左边的线画得慢,右边的线画得快。这个想法是让线条的宽度根据绘制的速度而变化,以使签名看起来更自然/真实。
我已经实现了一个自定义 PDFView,可以从云端加载 pdf 文件,也可以在本地加载 pdf 文件(如果可用)。对于本地实例,正如预期的那样,所有内容都加载得很快,但是当 url 不是本地的(即来自服务器的)时,可能需要一段时间,我想在 PDFView 加载文件时添加 UIActivityIndicator,有没有办法让我们知道一个代表或一个通知来监听以跟踪这一情况?
我的实现基本上如下所示:
let url = ReportsRepository.shared.getReportUrl(id: "1234")
self.pdfView.document = PDFDocument(url: url)
Run Code Online (Sandbox Code Playgroud)
此后,如果 URL 来自服务器,则应用程序似乎冻结,因此我需要在此处添加 UIActivityIndicator,问题是如何使用 PDFKit 停止它?
我正在 Swift 5 和 iOS 上工作。我正在尝试将文本覆盖到我当前拥有的 PDF 上。我本质上是在移植从 macOS 应用程序编写的代码。这是Mac版本的代码:
func executeContext(at srcURL: URL, to dstURL: URL) {
// Confirm there is a document there
if let doc: PDFDocument = PDFDocument(url: srcURL) {
// Create a document, get the first page, and set the size of the page
let page: PDFPage = doc.page(at: 0)!
var mediaBox: CGRect = CGRect(x: 0, y: 0, width: 792, height: 612)
// This is where the magic happens. Create the drawing context on the PDF …Run Code Online (Sandbox Code Playgroud) 我将使用 FastAPI 创建一个 API HTML,使用pdfkit. 但是,它将文件保存到我的本地磁盘。当我在线提供此API后,用户如何将该PDF文件下载到他们的计算机上?
from typing import Optional
from fastapi import FastAPI
import pdfkit
app = FastAPI()
@app.post("/htmltopdf/{url}")
def convert_url(url:str):
pdfkit.from_url(url, 'converted.pdf')
Run Code Online (Sandbox Code Playgroud) 我在 Python Flask 中有一段代码,可以使用 HTML 模板生成 pdf 文件。当我单独运行它时,代码运行得很好,但是当我尝试在 Docker 容器内运行它时,一旦我调用生成报告的端点,docker 就会崩溃并重置。它只是保持加载状态,然后返回错误(在我用来测试的 Postman 中)。
PDF的代码如下:
def create_report(download_uuid):
transactions = get_transaction_for_report(download_uuid)
config = pdfkit.configuration(wkhtmltopdf=environ.get('WKHTMLTOPDF'))
file_obj = io.BytesIO()
with zipfile.ZipFile(file_obj, 'w') as zip_file:
for transaction in transactions:
html = render_template("report.html", transaction=transaction)
pdf = pdfkit.from_string(html, False, configuration=config)
data = zipfile.ZipInfo('{}.pdf'.format(transaction['control_number']))
data.compress_type = zipfile.ZIP_DEFLATED
zip_file.writestr(data, pdf)
file_obj.seek(0)
return send_file(file_obj, attachment_filename="forms.zip", as_attachment=True)
Run Code Online (Sandbox Code Playgroud)
它返回一个 zip 文件,但 zip 文件内是 pdf 文件。此外,如果我删除 pdf 生成部分,zip 文件会正常返回。这是我的 Dockerfile:
FROM madnight/docker-alpine-wkhtmltopdf as wkhtmltopdf_image
FROM python:3.9-alpine
RUN adduser -D custom
WORKDIR /home/Project
COPY …Run Code Online (Sandbox Code Playgroud) 我正在尝试裁剪包含PDF的NSImage.在打印时我正在使用NSImage的drawInRect来让它只绘制我需要的东西 - 而且效果很好.
但是,现在我正试图创建一个仅裁剪区域的新NSImage.我玩了一段时间,然后在CocoaBuilder上找到了这个代码:
- (NSImage *) imageFromRect: (NSRect) rect
{
NSAffineTransform * xform = [NSAffineTransform transform];
// translate reference frame to map rectangle 'rect' into first quadrant
[xform translateXBy: -rect.origin.x
yBy: -rect.origin.y];
NSSize canvas_size = [xform transformSize: rect.size];
NSImage * canvas = [[NSImage alloc] initWithSize: canvas_size];
[canvas lockFocus];
[xform concat];
// Get NSImageRep of image
NSImageRep * rep = [self bestRepresentationForDevice: nil];
[rep drawAtPoint: NSZeroPoint];
[canvas unlockFocus];
return [canvas autorelease];
}
Run Code Online (Sandbox Code Playgroud)
这样可行,但返回的NSImage模糊,不再适合打印.有任何想法吗?
我想知道是否有一种方法可以在PDFKit(PDFDocument,PDFPage)中使用更简单的类,并且仍然可以获得对较低级别CGPDFDocumentRef对象的引用.有谁知道这是否可能?
在node.js上使用PDFKit:
var PDFDocument = require('pdfkit')
var doc = new PDFDocument()
doc.image('images/test.jpeg')
Run Code Online (Sandbox Code Playgroud)
如何将添加到PDF的图像居中?
使用PDFKit是可选的还是我需要使用另一个库?
我一直在网上搜索使用pdfkit(python包装器)实现页眉和页脚的人的例子,但找不到任何例子.
有人能够使用pdfkit python包装器展示如何在wkhtmltopdf中实现选项的一些示例吗?
pdfkit ×10
ios ×3
pdf ×3
wkhtmltopdf ×3
cocoa ×2
node.js ×2
python ×2
swift ×2
docker ×1
download ×1
fastapi ×1
flask ×1
ipad ×1
javascript ×1
node-pdfkit ×1
pdfview ×1
swift5 ×1
uibezierpath ×1