无法设置PDF边距以从iOS打印

时间:2014-05-21 11:21:37

标签: ios objective-c pdf

我在我的应用中显示UIWebView中的PDF。

- (void) startPrinting
{
    UIPrintInfo *pi = [UIPrintInfo printInfo];
    pi.outputType = UIPrintInfoOutputGeneral;
    pi.jobName = self.webView.request.URL.absoluteString;
    pi.orientation = UIPrintInfoOrientationPortrait;
    pi.duplex = UIPrintInfoDuplexLongEdge;

    UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
    pic.printInfo = pi;
    pic.showsPageRange = NO;
    pic.printFormatter = self.webView.viewPrintFormatter;
    UIPrintPageRenderer *renderer = [[UIPrintPageRenderer alloc] init];
    self.webView.viewPrintFormatter.printPageRenderer.headerHeight = 30.0f; //webView is the IBOutlet connected
    self.webView.viewPrintFormatter.printPageRenderer.footerHeight = 30.0f;
    self.webView.viewPrintFormatter.contentInsets = UIEdgeInsetsMake(0.0f, 30.0f, 0.0f, 30.0f);
    self.webView.viewPrintFormatter.startPage = 0;
    [renderer addPrintFormatter:self.webView.viewPrintFormatter startingAtPageAtIndex:0];
    pic.printPageRenderer = renderer;

    [pic presentAnimated:YES completionHandler:^(UIPrintInteractionController *pic2, BOOL completed, NSError *error) {
        NSLog(@"Completed status = %d and error status = %@", completed, error);
    }];
}

在NSObject类中,以下代码使用标准A4大小的纸张。

- (void)drawPDF:(NSString*)fileName
{
    // Create the PDF context using the default page size of 612 x 792.
    UIGraphicsBeginPDFContextToFile(fileName, CGRectZero, nil);
    // Mark the beginning of a new page.
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);

    //[self drawText:@"Hello World" inFrame:CGRectMake(0, 0, 300, 50)];

    [self drawLabels];
    [self drawLogo]; 

    // Close the PDF context and write out the contents.
    UIGraphicsEndPDFContext();

}

我已经加载了尺寸为612 x 792的笔尖来创建pdf。但是当我从打印机取出打印输出时,我得到了如下图所示的打印件。

enter image description here

如何将PDF调整大小到所需的边距?

1 个答案:

答案 0 :(得分:0)

我添加

后解决了这个问题
 UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 792, 1122), nil);

而不是

UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);

在drawPdf方法中。

另外,我将xib大小从612 x 792更改为792 x 1122,从而生成pdf。