有没有办法从 iOs 中的 XML/HTML 模板生成 PDF 文件 [英] Is there any way to generate PDF file from a XML/HTML template in iOs

查看:25
本文介绍了有没有办法从 iOs 中的 XML/HTML 模板生成 PDF 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 iPad 应用程序,因为我必须使用不同的 PDF 表单.我得到了一些使用 Quartz2D 通过代码生成 PDF 文件的方法.但是我必须通过代码编写整个表单.将来我可能需要更新 PDF 表单,所以我必须再次编写代码.所以我听说有一个名为 iTextSharp 的组件用于 .net pdf 创建 - iOS 有类似的东西吗?这样我就可以使用一些 XML 模板来创建 PDF 文件.请帮忙,谢谢

I am creating an iPad application, in that i have to use different PDF forms. I got some methods to generate PDF files through code using Quartz2D. But I have to write entire forms through code. I may have to update the PDF forms in future, so again I have to write the code. So I heard that there is a component called iTextSharp for .net pdf creation - is there something similar for iOS? So that I can use some XML templates to create the PDF files. Please help, thanks

推荐答案

我使用 iOS 打印子系统和 UIMarkupTextPrintFormatter 在我的应用程序中执行此操作.诀窍是编写您自己的自定义 UIPrintPageRenderer,覆盖并从 paperRect 和 numberOfPages 返回正确的值.您将把您的 UIMarkupTextPrintFormatter(s) 添加到您的自定义 UIPrintPageRenderer.

I do this in my app using the iOS print subsystem and the UIMarkupTextPrintFormatter. The trick is to write your own custom UIPrintPageRenderer that overrides and returns correct values from paperRect and numberOfPages. You'll add your UIMarkupTextPrintFormatter(s) to your custom UIPrintPageRenderer.

然后,在自定义 UIPrintPageRenderer 的上下文中,您将需要与此类似的例程:

Then, you'll need routines similar to this, in the context of your custom 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;
}

这篇关于有没有办法从 iOs 中的 XML/HTML 模板生成 PDF 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆