- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想在 UIKit 中动态生成一个最小的 PDF 页面,
所以直接发送到 AirPrint(不涉及文件),
其中仅包含一行文本,例如,
Hello, world
作为 Helvetica Neue Light 180pt。
TBC 它必须在 PDF 中实际排版,不呈现为图像。
请注意,渲染位图 的代码很简单且广泛可用.. 示例 https://stackoverflow.com/a/6566696/294884
我已经阅读并尝试过,直到我脸色发青。任何人都可以做到这一点吗?
PS 如果您正在阅读本文并且您是一名 Postscript 程序员,那么我专门讨论 iPad 中的 PDF 生成系统,例如:https://developer.apple.com/library/mac/documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_pdf/dq_pdf.html
(我完全不清楚 Quartz 是否是执行此操作的最佳方式 - 这是一场噩梦。)
顺便说一句,这是使用 html 方法执行此操作的确切代码...
此示例将一些动态 html 直接发送到 AirPrint,不涉及任何文件。
PDF 比较棘手
-(NSString*)_sample
{
NSString *r = @"<table border=0 cellpadding=0 cellspacing=0 width=100%>"
"<tr><td align=right>"
"<font face=courier size=1>Almost a header!</font>"
"</tr></td></table>"
"<br><br><br><br>"
"<font size=4 face=sans-serif>"
"Hello"
"<br><br><br><br>"
"Some <i>italics</i>."
"<br><br><br><br>"
"<table border=1 cellpadding=10 cellspacing=1>"
"<tr><td align=right>one</td><td>two</td><td>three</td></tr>"
"</table>"
"</font>";
return r;
}
-(IBAction)printRawHtml:(UIView *)sender
{
UIPrintInfo *pif = [UIPrintInfo printInfo];
pif.outputType = UIPrintInfoOutputGeneral;
pif.jobName = @"Test HTML-like Job";
UIMarkupTextPrintFormatter *formatter = [[UIMarkupTextPrintFormatter alloc]
initWithMarkupText:[self _sample]
];
formatter.startPage = 0;
formatter.contentInsets = UIEdgeInsetsMake(10, 72.0, 72.0, 72.0);
//top,left,bottom,right
UIPrintInteractionController *pic =
[UIPrintInteractionController sharedPrintController];
pic.delegate = self;
pic.printInfo = pif;
pic.printFormatter = formatter;
pic.showsPageRange = NO;
pic.showsNumberOfCopies = NO;
void (^cph)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController
*printController, BOOL completed, NSError *error)
{
if (!completed && error) NSLog(@"print error %@", error);
};
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
[pic presentFromRect:sender.frame
inView:self.view animated:YES completionHandler:cph];
else
[pic presentAnimated:YES completionHandler:cph];
}
最佳答案
Apple 开发者网站的代码
- (IBAction)savePDFFile:(id)sender
{
// Prepare the text using a Core Text Framesetter.
CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, (CFStringRef)textView.text, NULL);
if (currentText) {
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);
if (framesetter) {
NSString *pdfFileName = [self getPDFFileName];
// Create the PDF context using the default page size of 612 x 792.
UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);
CFRange currentRange = CFRangeMake(0, 0);
NSInteger currentPage = 0;
BOOL done = NO;
do {
// Mark the beginning of a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);
// Draw a page number at the bottom of each page.
currentPage++;
[self drawPageNumber:currentPage];
// Render the current page and update the current range to
// point to the beginning of the next page.
currentRange = [self renderPageWithTextRange:currentRange andFramesetter:framesetter];
// If we're at the end of the text, exit the loop.
if (currentRange.location == CFAttributedStringGetLength((CFAttributedStringRef)currentText))
done = YES;
} while (!done);
// Close the PDF context and write the contents out.
UIGraphicsEndPDFContext();
// Release the framewetter.
CFRelease(framesetter);
} else {
NSLog(@"Could not create the framesetter needed to lay out the atrributed string.");
}
// Release the attributed string.
CFRelease(currentText);
} else {
NSLog(@"Could not create the attributed string for the framesetter");
}
}
这是Apple的官方文档
关于iOS 将 "Hello, world"(Helv.Neue Light, 180pt) 渲染为 PDF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24480617/
例如 int (*pt)[4]; int target[3][4] = {{1,3,4,6},{2,4,5,8},{3,4,6,7}}; pt = target; *p
如果您有本地化的应用程序 pt-br和 pt-pt , 如果系统只报告 pt 应该选择什么语言代码(通用葡萄牙语)? 这个问题与应用程序的性质、桌面、移动或基于浏览器无关。让我们假设您无法获取区域信息
我写了以下代码: typedef int* pt; pt p = pt(); // compile ok 但是 int* p = (int*)() // compile fail. 我想知道
这个问题在这里已经有了答案: What makes more sense - char* string or char *string? [duplicate] (13 个答案) 关闭 9 年前。
符号?在下面的代码中是什么意思: for(int i = 0 ; i pt.X) ? pt.X : minX; minY = (minY > pt.Y) ? pt.Y : min
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我构建了一个倒数计时器,但它只是使用 getTime(),我不确定如何调整它,以便它是我想要的正确时区(PDT/PT) var countdownTimer = setInterval(countdo
pt-kill 是一个优秀的kill MySQL连接的一个工具,是percona toolkit的一部分,在因为空闲连接较多导致超过最大连接数、某个有问题的sql导致mysql负载很高时,都需要将一
作为我以前的question的后续操作,如果我想让我的智能手机应用程序检测到某个音符,并且只需要知道传入的声音是否是该音符且带有一定的模糊性,就可以允许注意偏离x美分。 鉴于此,在速度和准确性上是否有
这个问题以一种令人困惑的方式演变。不过,它的某些部分,特别是一些答案,可能对某人有用。因此,我将不修改此问题,并尝试重新表述问题 here . 覆盖模板 folder_full_view_item.p
所以在我最后一个问题的代码中是一个错误。我尝试修改代码,将两个大数字添加为两个数组(我不能为此使用 BigIntiger,我必须自己创建该方法)。但它仍然给我错误的加法结果。 例如(我已经有这个的构造
我似乎无法从新用户发送的新消息或从已在表格 View 中的用户发送的新消息转到大多数消息应用程序的类似位置。我以前问过这个问题,并尝试按照好心人发给我的链接中的说明进行操作( How do you p
我有一个昂贵的报告查询,可能需要 1-20 秒以上的时间才能运行。 (取决于有多少数据) 有没有办法在一定时间后终止运行的 mysql 进程/查询? 我看到这个: mysql auto kill qu
我正在尝试使用 pt-archiver 清除 mysql 表,但遇到了一个奇怪的问题。 假设以下查询返回 60789 行。 SELECT COUNT(*) FROM abc WHERE created
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: Are there any practical reasons to use “em” instead of
这几天我在做移动webApp开发,我读了一些关于它的文章,但是描述here把我弄糊涂了。 Points are abstract units, they only make sense in this
我正在使用 Visual Studio,出于某种原因,我渲染的字体显示为 Arial,即使我将字体系列设置为 font-family: 'PT Sans', sans-serif; 这是为什么?这不是
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及预
我正在阅读“与 Rcpp 无缝集成 R 和 C++”的第 4 章,我遇到了一个小问题。 在“ list 4.13”这本书中给出了一个关于如何使用 R 函数的示例。我尝试使用其他函数(与示例不同)并且我
我正在使用 jQueryMobile 和 PhoneGap 开发一个跨平台的移动应用程序。我的第一个目标平台是 Android。 对于Android应用,建议开发者使用dp作为字体大小的单位。 DP代
我是一名优秀的程序员,十分优秀!