gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:32:52 25 4
gpt4 key购买 nike

我想知道如何从 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 等,但他们得到了解决方案,但我得到了错误..

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

请帮帮我..

最佳答案

//在这里传递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;
}

关于iphone - 如何从 HTML 页面获取 PDF (UIWebView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16932059/

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