gpt4 book ai didi

iOS 将 "Hello, world"(Helv.Neue Light, 180pt) 渲染为 PDF?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:00:33 24 4
gpt4 key购买 nike

我想在 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的官方文档

https://developer.apple.com/library/ios/documentation/2ddrawing/conceptual/drawingprintingios/GeneratingPDF/GeneratingPDF.html

关于iOS 将 "Hello, world"(Helv.Neue Light, 180pt) 渲染为 PDF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24480617/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com