从 plist 文件生成 PDF(包含文本和图像),可以吗?

发布于 2024-10-08 05:03:05 字数 344 浏览 0 评论 0原文

背景:

我的应用程序将用户输入的信息保存到 plist 文件中。当拍摄照片或从库中选择照片时,照片也会作为 NSData 保存到 plist 中。

我想获取 plist 文件中包含的信息并生成 PDF 文档,包括来自 NSData 的图像。

问题:

我听说有一种方法可以从 UIView 生成 PDF,是否有一个很好的示例,或者一种无需编写 1000 行基于 C 的代码的简单方法?

是否可以在 Interface Builder 中设计一个视图,然后使用一些神奇的代码,使用 plist 文件中的数据从该视图生成 PDF?

Background:

My app saves user-inputted information into plist files. When a photo is taken, or selected from the library, the photo is saved as NSData to the plist as well.

I want to take the information contained in the plist files and generate a PDF document, including the image from NSData.

Question:

I've heard there is a way to generate a PDF from a UIView, is there a nice example to this, or an easy way without writing 1000 lines of C-based code?

Is it possible to design a view in Interface Builder, then use some magic code to generate a PDF from that view, using the data from a plist file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夢归不見 2024-10-15 05:03:05

要生成 PDF,您必须实际绘制整个 PDF,将图形和文本放置在每个页面上的确切位置。 Apple 的绘图和打印指南 解释了如何进行。

不幸的是,这并不容易,而且我不知道有什么神奇的代码可以从视图生成 PDF。

To generate a PDF you have to actually draw out the whole PDF, putting the graphics and text in exact places on each page. Apple's Drawing and Printing Guide explains how to go about it.

It's not easy, unfortunately, and I'm not aware of any magic code that will generate a PDF from a view.

暖心男生 2024-10-15 05:03:05

这里有一些“神奇代码”(引用@Matt;-),它将把视图层次结构写入 PDF:

#import <QuartzCore/CALayer.h>

UIView *displayView = self.view;
CALayer *layer = displayView.layer;
CGRect pageRect = displayView.bounds;
if (UIGraphicsBeginPDFContextToFile(path, pageRect, [self pdfContextDictionary]) == NO) {
  return nil; // error
}

UIGraphicsBeginPDFPage();
CGContextRef context = UIGraphicsGetCurrentContext();
[layer renderInContext:context];
UIGraphicsEndPDFContext();  

不幸的是,它在屏幕分辨率下这样做,
您可以创建一个比屏幕更大的版本,那么它将是一个更好的分辨率,

为了漂亮、漂亮(和痛苦)的 PDF,请参阅上面引用的指南

here's some "magic code" (to quote @Matt ;-) that will write a view hierarchy into a PDF:

#import <QuartzCore/CALayer.h>

UIView *displayView = self.view;
CALayer *layer = displayView.layer;
CGRect pageRect = displayView.bounds;
if (UIGraphicsBeginPDFContextToFile(path, pageRect, [self pdfContextDictionary]) == NO) {
  return nil; // error
}

UIGraphicsBeginPDFPage();
CGContextRef context = UIGraphicsGetCurrentContext();
[layer renderInContext:context];
UIGraphicsEndPDFContext();  

unfortunately, it does so at screen resolution,
you can create a larger-than-screen version, then it will be a better resolution,

for nice, pretty, (and painful) PDF, see the above referenced guide

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文