So I have tried a couple routes here, but there always seems to be a trade off.
What I have is a vertically long PDF (Contained within a subclass of NSDocument
), and for example I will be using a screenshot of a16z. This is my current result of printing then choosing the "Save to PDF" option.
https://www.dropbox.com/s/02u3j8pbw1kne91/Untitled.pdf?dl=0
There are two issues with this:
1) The image seems to be stretched vertically a little bit
2) When I actually make a physical print, it only grabs the bottom left corner, and only part of the pdf as well:
So here is the pertinent code (At least I believe, there could be more in other layers but I've mostly been playing around here):
EditorPrintView *offscreenView = [[EditorPrintView alloc] initWithFrame:[self canvasRect] withImageDoc:self withBackgroundColor:[self backgroundColor]];
NSPrintInfo *printInfo = [self printInfo];
[printInfo setVerticalPagination:NSAutoPagination];
[printInfo setPaperSize:NSMakeSize[self canvasRect].size.width , ([self canvasRect].size.height+[[self printInfo] topMargin] + [[self printInfo] bottomMargin]))];
[self setPrintInfo:printInfo];
NSPrintOperation *op = [NSPrintOperation printOperationWithView:offscreenView
printInfo:printInfo];
[op runOperationModalForWindow:[[EditorWindowController sharedWindowController] window]
delegate:self
didRunSelector:@selector(printOperationDidRun:success:contextInfo:)
contextInfo:nil];
Now here's where things get funky.
I have been making changes to the paperSize
with NSMakeSize[self canvasRect].size.width , ([self canvasRect].size.height+[[self printInfo] topMargin] + [[self printInfo] bottomMargin]))
to produce different results with the printing.
Here have been my experiments:
1) If I do not set the paperSize
at all, the pdf actually paginates correctly:
The number of pages is correct, but, the content is incorrect. Each page gets the entire image squished into one page.
2) If I set the paperSize
to be the size of the image, 2 pages get produced. One page with the correct size (the page size is huge):
, and then another page with the entire image squished into the vertical size of the margins of the page:
3) So naturally, I decided to go with the code I have now, and added the margins when I set paperSize
.
This results in the one page pdf that I linked initially-- but again, it seems to be stretched vertically, and also doesn't actually print correctly
Knowing all of this, I believe that I do not want to mess with the paperSize
so I can maintain the correct pagination, and maintain a normal paper size.
I believe that perhaps I am doing something wrong with the NSView
that I am supplying. Those with a keen eye would see that I have a subclassed NSView
called EditorPrintView
. Here is the -drawRect
method:
- (void)drawRect:(NSRect)rect
{
if ( (_editorDoc) && (_backgroundColor) )
{
NSGraphicsContext* theContext = [NSGraphicsContext currentContext];
if ( [theContext isDrawingToScreen] == NO )
{
[theContext saveGraphicsState];
ImageDocumentRenderer* renderer = [ImageDocumentRenderer pixelRenderForDocumentData:_editorDoc.docData andDownsample:NO];
CGLayerRef cgLayer = [renderer createPrinterFriendlyCGLayerWithContext:(CGContextRef)[theContext graphicsPort] andBackgroundColor:_backgroundColor];
CGContextDrawLayerInRect((CGContextRef)[theContext graphicsPort], rect, cgLayer);
[theContext restoreGraphicsState];
CGLayerRelease(cgLayer);
}
else
{
NSLog(@"EditorPrintView drawRect - called for drawing onpo [ screen, should only be used for printing");
}
}
else
{
NSLog(@"EditorPrintView drawRect - editordocument and/or background color never set, NOT DRAWING ANYTHING");
}
}
This is where the real wall comes up, because I have no idea how to debug CGContext, and the initial engineer is gone from the company so I can't ask anybody. Are there any red flags anybody can see with how I am drawing my NSView
?
I know this is a long one, so cheers in advance for your time reading :)