Tôi có phương pháp này ở đây có UIWebView của tôi và chuyển đổi thành PDF và hoạt động tốt. Nhưng khi tôi in ra PDF này hoặc gửi email, nó bị cắt. Nó giống như nó chỉ tạo ra kích thước của UIWebView mà tôi đặt (chiều rộng: 688 & chiều cao: 577) Nếu tôi tăng kích thước của UIWebView để cho phép nói 900 hoặc 1024 tệp PDF của tôi trống. UIWebView của tôi lớn hơn 577, nhưng trong ứng dụng của tôi, tôi có thể cuộn.Các vấn đề mục tiêu-C với UIWebView sang PDF
Đây là phương pháp ....
-(void)webViewDidFinishLoad:(UIWebView *)webViewPDF
{
CGRect origframe = webViewPDF.frame;
NSString *heightStr = [webViewPDF stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"]; // Get the height of our webView
int height = [heightStr intValue];
CGFloat maxHeight = kDefaultPageHeight - 2*kMargin;
int pages = floor(height/maxHeight);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
self.pdfPath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"Purchase Order.pdf"]];
UIGraphicsBeginPDFContextToFile(self.pdfPath, CGRectZero, nil);
for (int i = 0; i < pages; i++)
{
if (maxHeight * (i+1) > height) {
CGRect f = [webViewPDF frame];
f.size.height -= (((i+1) * maxHeight) - height);
[webViewPDF setFrame: f];
}
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kDefaultPageWidth, kDefaultPageHeight), nil);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(currentContext, kMargin, kMargin);
[webViewPDF.layer renderInContext:currentContext];
}
UIGraphicsEndPDFContext();
[webViewPDF setFrame:origframe];
[[[webViewPDF subviews] lastObject] setContentOffset:CGPointMake(0, 0) animated:NO];
}
Tôi hy vọng điều này có ý nghĩa .... Có ai có bất kỳ gợi ý về cách để sửa lỗi này, vì vậy PDF được không cắt đứt?
tôi quên đề cập đến các biến:
#define kDefaultPageHeight 850
#define kDefaultPageWidth 850
#define kMargin 50
Đây là nút chia sẻ của tôi:
- (IBAction)Share:(id)sender {
NSData *pdfData = [NSData dataWithContentsOfFile:self.pdfPath];
UIActivityViewController * activityController = [[UIActivityViewController alloc] initWithActivityItems:@[pdfData] applicationActivities:nil];
UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:activityController];
[popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width - 36, 60, 0, 0)inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
Vậy điều này có liên quan gì đến Objective-C? – matt
phương pháp này là Mục tiêu-C – user979331
Rõ ràng mã của bạn sẽ xảy ra trong mục tiêu-C. Bất cứ ai cũng có thể thấy điều đó. Nhưng câu hỏi không phải là _about_ Objective-C. Dù câu trả lời là như thế nào cho dù bạn đang sử dụng Objective-C, Swift, AppleScript hay bất kỳ thứ gì. – matt