我想在UIKit中动态生成一个最小的PDF页面,
所以直接发送到AirPrint(不涉及文件),
只包含一行文字,例如
你好,世界
作为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];
}
答案 0 :(得分:2)
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的官方文档
答案 1 :(得分:1)
与提及的@luser droog一样,您可以使用Quartz2D在iOS中生成PDF。我从未尝试过自己,但你可以查看this tutorial by Ray Wenderlich。他的教程详细介绍了如何以编程方式创建PDF以显示简单的“Hello Hello&#34;字符串。