Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa PDF page splitting

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 didFinishLoadForFrame:(WebFrame *)frame
{
    if ([frame isEqual:[[self doc] mainFrame]]) 
    {
        NSMutableData *newData = [[NSMutableData alloc] init];
        NSPrintInfo *newInfo = [NSPrintInfo sharedPrintInfo];
        NSView *docView = [[[[self doc] mainFrame] frameView] documentView];

        NSPrintOperation *newPrintOp = [NSPrintOperation PDFOperationWithView:docView insideRect:docView.bounds toData:newData printInfo:newInfo];

        BOOL runPrint = [newPrintOp runOperation];  
        if (!runPrint)
        {
           NSLog(@"Print Failed");
        }
        PDFDocument *newDoc = [[PDFDocument alloc] initWithData:newData];
        [newData release];
        [self setPdf:newDoc];

        //Other code here
        }
    }

The problem is that when I look at newDoc, it is a huge PDF of a single page. What I would prefer would be the printing acting the same as it does from the "save as PDF..." dialog - that is, splitting the PDF into multiple reasonably-sized pages.

Does anyone know how to accomplish this?

I attempted inserting the following after NSPrintInfo *newInfo = [NSPrintInfo sharedPrintInfo];

[newInfo setVerticalPagination:NSAutoPagination];
[newInfo setHorizontalPagination:NSAutoPagination];

NSAutoPagination is described in the docs as the following:

NSAutoPagination The image is divided into equal-sized rectangles and placed in one column of pages. Available in Mac OS X v10.0 and later. Declared in NSPrintInfo.h.

This had no effect on the printed PDF.

like image 882
Daniel Avatar asked Dec 23 '11 06:12

Daniel


1 Answers

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.
}
like image 145
dzolanta Avatar answered Nov 21 '22 03:11

dzolanta