如何从HTML页面获取PDF(UIWebView)

时间:2013-06-05 05:17:56

标签: iphone ios objective-c ios5 airprint

我想知道如何从HTML页面获取PDF。

我正在使用方法

UIGraphicsBeginPDFContextToFile(self.filePath, CGRectZero, nil);    //creating PDF

    int i = 0;
    for (; i < pages; i++)
    {
        if (pageHeight * (i+1) > height)
        {
            CGRect f = [myWebPage frame];
            f.size.height -= (((i+1) * pageHeight) - height);
            [myWebPage setFrame: f];
        }
        // Specify the size of the pdf page
        UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, width, pageHeight), nil);
        CGContextRef currentContext =  UIGraphicsGetCurrentContext();
        [[[myWebPage subviews] lastObject] setContentOffset:CGPointMake(0, pageHeight * i) animated:NO];
        [myWebPage.layer renderInContext: currentContext];
        NSLog(@"I = %d",i);
    }

UIGraphicsEndPDFContext();
[[[myWebPage subviews] lastObject] setContentOffset:CGPointMake(0, 0) animated:NO];
[myWebPage setFrame:origframe];

但是这不会给出正确的PDF,因为多段PDF的某些部分会被剪切掉,并且如果它超过10秒就会崩溃应用程序。

我已下载https://github.com/iclems/iOS-htmltopdf此文件并使用NDHTMLtoPDF.h和NDHTMLtoPDF.m文件..

工作正常..

但是当我尝试使用像

这样的个别功能时
 - (void)webViewDidFinishLoad:(UIWebView *)webView
{
if (webView.isLoading) return;

UIPrintPageRenderer *render = [[UIPrintPageRenderer alloc] init];

[render addPrintFormatter:webView.viewPrintFormatter startingAtPageAtIndex:0];

CGRect printableRect = CGRectMake(self.pageMargins.left,
                              self.pageMargins.top,
                              self.pageSize.width - self.pageMargins.left - self.pageMargins.right,
                              self.pageSize.height - self.pageMargins.top - self.pageMargins.bottom);

CGRect paperRect = CGRectMake(0, 0, self.pageSize.width, self.pageSize.height);

[render setValue:[NSValue valueWithCGRect:paperRect] forKey:@"paperRect"];
[render setValue:[NSValue valueWithCGRect:printableRect] forKey:@"printableRect"];

NSData *pdfData = [render printToPDF];

[pdfData writeToFile: self.PDFpath  atomically: YES];

[self terminateWebTask];

if (self.delegate && [self.delegate respondsToSelector:@selector(HTMLtoPDFDidSucceed:)])
    [self.delegate HTMLtoPDFDidSucceed:self];


}

- (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;
}

显示错误

NSData *pdfData = [render printToPDF];
[self prepareForDrawingPages: NSMakeRange(0, self.numberOfPages)];
[self drawPageAtIndex: i inRect: bounds];

因为它无法找到函数和变量。

但是在代码中他们并没有声明任何变量,比如numberOfPages和所有变量,但他们正在获得解决方案,但我得到了错误..

任何人都可以向我发送正确的代码和步骤,以便将HTML文件的打印格式化为PDF格式,使用UIPrintPageRenderer。

请帮帮我..

1 个答案:

答案 0 :(得分:2)

//在此处传递HTML字符串

 NSString *str_PDFName_path = [NSString stringWithFormat:@"~/Library/MyFile.pdf"];
    self.PDFCreator = [NDHTMLtoPDF createPDFWithHTML:str_HTML pathForPDF:[str_PDFName_path stringByExpandingTildeInPath] delegate:self pageSize:kPaperSizeA4 margins:UIEdgeInsetsMake(20, 5, 90, 5)];

//执行此操作后

- (NSData*) printToPDF
{
    NSMutableData *pdfData = [NSMutableData data];
    //595, 842
    UIGraphicsBeginPDFContextToData(pdfData, CGRectMake(0, 20, 595, 842), nil);

   // [self prepareForDrawingPages: NSMakeRange(0, self.numberOfPages)];

    //CGRect bounds = UIGraphicsGetPDFContextBounds();

    for ( int i = 0 ; i < self.numberOfPages ; i++ )
    {
        UIGraphicsBeginPDFPage();

        [self drawPageAtIndex: i inRect: CGRectMake(120, 770, 500, 15)]; //bounds];
        //CGRect textRect = [self addText:@"Centurion Medical Products | 100 Centurion Way | Williamston MI USA 48895 | 800.248.4058"
        //                      withFrame:CGRectMake(120, 770, 500, 15) fontSize:8.0f];
        CGContextRef currentContext = UIGraphicsGetCurrentContext();
        CGContextSetRGBFillColor(currentContext, 99/255.f, 100/255.f, 99/255.f, 1.0f);

        [self addText:@"Medical Products | 100 Centurion Way | Williamston MI USA 48895 | 800.248.4058"
                              withFrame:CGRectMake(130, 770, 500, 15) fontSize:8.0f];
        UIImage *anImage = [UIImage imageNamed:@"logo.gif"];
        //CGRect imageRect = [self addImage:anImage
        //                          atPoint:CGPointMake(50, 768)];
        [self addImage:anImage atPoint:CGPointMake(50, 768)];
    }

    UIGraphicsEndPDFContext();

    return pdfData;
}