Cocoa PDF page splitting

后端 未结 1 1711
挽巷
挽巷 2020-12-15 12:58

In the application I\'m creating, I load a long page of HTML into a webView and then print it to a PDF using the following:

-(void)webView:(WebView *)sender          


        
相关标签:
1条回答
  • 2020-12-15 13:21

    You get a file with one large page because + PDFOperationWithView: method doesn't support pagination at all. For that reason calling - setVerticalPagination: or - setHoriziontalPagination: doesn't change anything.

    You could try use "classical" + printOperationWithView:printInfo: method, configure it to save PDF to temporary location and then create PDFDocument with contents of obtained file. I hope that fragment of code below will help.

    NSMutableDictionary *dict = [[NSPrintInfo sharedPrintInfo] dictionary];
    [dict setObject:NSPrintSaveJob forKey:NSPrintJobDisposition];
    [dict setObject:temporaryFilePath forKey:NSPrintSavePath];
    NSPrintInfo *pi = [[NSPrintInfo alloc] initWithDictionary:dict];
    [pi setHorizontalPagination:NSAutoPagination];
    [pi setVerticalPagination:NSAutoPagination];
    
    NSPrintOperation *op = [NSPrintOperation printOperationWithView:[[[webView mainFrame] frameView] documentView] printInfo:pi];
    [pi release];
    [op setShowsPrintPanel:NO];
    [op setShowsProgressPanel:NO];
    
    if ([op runOperation] ){
        PDFDocument *doc = [[[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath: temporaryFilePath]] autorelease];
        // do with doc what you want, remove file, etc.
    }
    
    0 讨论(0)
提交回复
热议问题