我正在创建一个iPad应用程序,因为我必须使用不同的PDF表单。我有一些使用Quartz2D通过代码生成PDF文件的方法。但我必须通过代码编写完整的表单。我将来可能需要更新PDF表单,所以我必须再次编写代码。所以我听说有一个名为iTextSharp的组件用于.net pdf创建 - iOS有类似的东西吗?这样我就可以使用一些XML模板来创建PDF文件。 请帮助,谢谢
答案 0 :(得分:14)
我使用iOS打印子系统和UIMarkupTextPrintFormatter在我的应用程序中执行此操作。诀窍是编写自己的自定义UIPrintPageRenderer,它覆盖并返回paperRect和numberOfPages中的正确值。您将UIMarkupTextPrintFormatter添加到自定义UIPrintPageRenderer。
然后,在自定义UIPrintPageRenderer的上下文中,您将需要与此类似的例程:
- (CGRect) paperRect
{
if (!_generatingPdf)
return [super paperRect];
return UIGraphicsGetPDFContextBounds();
}
- (CGRect) printableRect
{
if (!_generatingPdf)
return [super printableRect];
return CGRectInset( self.paperRect, 20, 20 );
}
- (NSData*) printToPDF
{
_generatingPdf = YES;
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData( pdfData, CGRectMake(0, 0, 792, 612), nil ); // letter-size, landscape
[self prepareForDrawingPages: NSMakeRange(0, 1)];
CGRect bounds = UIGraphicsGetPDFContextBounds();
for ( int i = 0 ; i < self.numberOfPages ; i++ )
{
UIGraphicsBeginPDFPage();
[self drawPageAtIndex: i inRect: bounds];
}
UIGraphicsEndPDFContext();
_generatingPdf = NO;
// NSString* filename = @"/Volumes/Macintosh HD 2/test.pdf";
// [pdfData writeToFile: filename atomically: YES];
return pdfData;
}
答案 1 :(得分:1)
@TomSwift:同意他的回答,但我想以更好的方式解释
PDF
创建UIWebView
,可以将HTML加载到yourWebView,或者也可以隐藏yourWebView来生成PDF但是进程保持不变Category
UIPrintPageRenderer
获取PDF数据作为您的WebView.viewPrintFormatter需要使用@interface UIPrintPageRenderer (PDF)
- (NSData*) printToPDF;
@end
@implementation UIPrintPageRenderer (PDF)
- (NSData*) printToPDF
{
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData( pdfData, self.paperRect, nil );
[self prepareForDrawingPages: NSMakeRange(0, self.numberOfPages)];
CGRect bounds = UIGraphicsGetPDFContextBounds();
for ( int i = 0 ; i < self.numberOfPages ; i++ )
{
UIGraphicsBeginPDFPage();
[self drawPageAtIndex: i inRect: bounds];
}
UIGraphicsEndPDFContext();
return pdfData;
}
@end
#define kPaperSizeA4 CGSizeMake(595.2,841.8)
//create print renderer
UIPrintPageRenderer *render = [[UIPrintPageRenderer alloc] init];
[render addPrintFormatter:yourWebView.viewPrintFormatter startingAtPageAtIndex:0];
//provide padding ---- increase these values according to your requirement
float topPadding = 10.0f;
float bottomPadding = 10.0f;
float leftPadding = 10.0f;
float rightPadding = 10.0f;
//provide rect for printing and for actual PDF Rect of page
CGRect printableRect = CGRectMake(leftPadding,
topPadding,
kPaperSizeA4.width-leftPadding-rightPadding,
kPaperSizeA4.height-topPadding-bottomPadding);
CGRect paperRect = CGRectMake(0, 0, kPaperSizeA4.width, kPaperSizeA4.height);
[render setValue:[NSValue valueWithCGRect:paperRect] forKey:@"paperRect"];
[render setValue:[NSValue valueWithCGRect:printableRect] forKey:@"printableRect"];
//category created above is used here
NSData *pdfData = [render printToPDF];
//Save PDF to directory for usage
if (pdfData) {
[pdfData writeToFile:[NSString stringWithFormat:@"%@/tmp.pdf",NSTemporaryDirectory()] atomically: YES];
}
else
{
NSLog(@"PDF couldnot be created");
}