在 iPad 应用程序中从 UIView 创建 pdf 时出现问题

发布于 2024-11-08 13:58:48 字数 1849 浏览 6 评论 0原文

我正在 iPad 应用程序中从 UIView 创建 pdf。它的大小为 768 * 2000。当我创建 pdf 时,它会以相同的大小创建,并在一页上显示所有内容。所以当我从 iPad 打印它时遇到问题。我正在使用以下代码来创建 pdf:-

-(void)drawPdf:(UIView *)previewView{   
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"Waypoint Data.pdf"];
    //CGRect tempRect = CGRectMake(0, 0, 768, 1068);
    CGContextRef pdfContext = [self createPDFContext:previewView.bounds path:(CFStringRef)writableDBPath];
    CGContextBeginPage (pdfContext,nil); // 6

    //turn PDF upsidedown

    CGAffineTransform transform = CGAffineTransformIdentity;    
    transform = CGAffineTransformMakeTranslation(0, previewView.bounds.size.height);
    transform = CGAffineTransformScale(transform, 1.0, -1.0);
    CGContextConcatCTM(pdfContext, transform);

    //Draw view into PDF
    [previewView.layer renderInContext:pdfContext]; 
    CGContextEndPage (pdfContext);// 8
    CGContextRelease (pdfContext);  
}

//Create empty PDF context on iPhone for later randering in it

-(CGContextRef) createPDFContext:(CGRect)inMediaBox path:(CFStringRef) path{

    CGContextRef myOutContext = NULL;

    CFURLRef url;

    url = CFURLCreateWithFileSystemPath (NULL, // 1

                                     path,

                                     kCFURLPOSIXPathStyle,

                                     false);

    if (url != NULL) {

        myOutContext = CGPDFContextCreateWithURL (url,// 2

                                              &inMediaBox,                                                NULL);        
        CFRelease(url);// 3     
    }   
    return myOutContext;// 4    
} 

谁能建议我如何减小 pdf 大小并且它有多个页面?

提前致谢。

I am creating a pdf from a UIView in my iPad Application. It has size 768 * 2000. When I create pdf then it create with same size and it shows all content on one page. So I am facing problem when I print it from iPad. i am using the following code for creating pdf :-

-(void)drawPdf:(UIView *)previewView{   
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"Waypoint Data.pdf"];
    //CGRect tempRect = CGRectMake(0, 0, 768, 1068);
    CGContextRef pdfContext = [self createPDFContext:previewView.bounds path:(CFStringRef)writableDBPath];
    CGContextBeginPage (pdfContext,nil); // 6

    //turn PDF upsidedown

    CGAffineTransform transform = CGAffineTransformIdentity;    
    transform = CGAffineTransformMakeTranslation(0, previewView.bounds.size.height);
    transform = CGAffineTransformScale(transform, 1.0, -1.0);
    CGContextConcatCTM(pdfContext, transform);

    //Draw view into PDF
    [previewView.layer renderInContext:pdfContext]; 
    CGContextEndPage (pdfContext);// 8
    CGContextRelease (pdfContext);  
}

//Create empty PDF context on iPhone for later randering in it

-(CGContextRef) createPDFContext:(CGRect)inMediaBox path:(CFStringRef) path{

    CGContextRef myOutContext = NULL;

    CFURLRef url;

    url = CFURLCreateWithFileSystemPath (NULL, // 1

                                     path,

                                     kCFURLPOSIXPathStyle,

                                     false);

    if (url != NULL) {

        myOutContext = CGPDFContextCreateWithURL (url,// 2

                                              &inMediaBox,                                                NULL);        
        CFRelease(url);// 3     
    }   
    return myOutContext;// 4    
} 

Can anyone suggest me how can I reduce the pdf size and it has multiple page ?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听风吹 2024-11-15 13:58:48

请参阅“iOS 绘图和打印指南”中的示例

https://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GeneratePDF/GeneratePDF.html#//apple_ref/doc/uid/TP40010156-CH10-SW1

do while 循环,并注意它如何在循环中启动一个新的 PDF 页面

……

// Mark the beginning of a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);

基本上在清单 4-1 代码示例中,他们有一个

: 在您当前的方法中,您只调用了开始页面方法之一,因此这就是为什么您只会有一个页面。

See example in the "Drawing and Printing Guide for iOS"

https://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GeneratingPDF/GeneratingPDF.html#//apple_ref/doc/uid/TP40010156-CH10-SW1

Basically in the Listing 4-1 code example, they have a do while loop and take notice how it starts a new PDF page in the loop:

...

// Mark the beginning of a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);

...

In your current method, you only called one of the begin page methods once so that's why you'll only ever have one page.

傲性难收 2024-11-15 13:58:48

您需要为要创建的每个新 PDF 页面调用 UIGraphicsBeginPDFPage。假设您有一个可变高度的 UIView,以下是您在运行时根据需要将其分解为尽可能多的 PDF 页面的方法:

NSInteger pageHeight = 792; // Standard page height - adjust as needed
NSInteger pageWidth = 612; // Standard page width - adjust as needed

/* CREATE PDF */
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, CGRectMake(0,0,pageWidth,pageHeight), nil);
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
for (int page=0; pageHeight * page < theView.frame.size.height; page++)
{
    UIGraphicsBeginPDFPage();
    CGContextTranslateCTM(pdfContext, 0, -pageHeight * page);
    [theView.layer renderInContext:pdfContext];
}

UIGraphicsEndPDFContext();

You need to call on UIGraphicsBeginPDFPage for every new PDF page you want to create. Assuming you have a UIView of variable height, here is how you would break it down to as many PDF pages as needed during run time:

NSInteger pageHeight = 792; // Standard page height - adjust as needed
NSInteger pageWidth = 612; // Standard page width - adjust as needed

/* CREATE PDF */
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, CGRectMake(0,0,pageWidth,pageHeight), nil);
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
for (int page=0; pageHeight * page < theView.frame.size.height; page++)
{
    UIGraphicsBeginPDFPage();
    CGContextTranslateCTM(pdfContext, 0, -pageHeight * page);
    [theView.layer renderInContext:pdfContext];
}

UIGraphicsEndPDFContext();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文