title: Cocoa - Read PDF files from file system and print them
body: <p>I'm trying to print PDF-files that are read from the file system. But can't quite get it to work. I know that I have to have the file in a view and then print the view.</p>
  
  <p>This is my print method:</p>
  
  <pre><code>NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
  NSPrintOperation *PDFPrint = [NSPrintOperation printOperationWithView:_pdfView printInfo:printInfo];
  [PDFPrint setCanSpawnSeparateThread:YES];
  [PDFPrint setShowsProgressPanel:YES];
  [PDFPrint runOperation];
  </code></pre>
  
  <p>And this is how I create the view:</p>
  
  <pre><code>PDFDocument *pdfDocument = [[PDFDocument alloc] initWithURL:fileURL];
  
  PDFPage *firstPage = [pdfDocument pageAtIndex:0];
  NSRect bounds = [firstPage boundsForBox:kPDFDisplayBoxMediaBox];
  NSSize pixelSize = bounds.size;
  NSSize documentSize;
  
  documentSize.width = pixelSize.width;
  documentSize.height = pixelSize.height;
  PDFView *PDFDocumentView = [[PDFView alloc] initWithFrame:NSMakeRect(0, 0, documentSize.width, documentSize.height)];
  
  [PDFDocumentView setDisplayMode:kPDFDisplaySinglePage];
  [PDFDocumentView setDisplayBox:kPDFDisplayBoxMediaBox];
  [PDFDocumentView setAutoScales:NO];
  [PDFDocumentView setDocument:pdfDocument];
  [PDFDocumentView setBackgroundColor:[NSColor redColor]];
  
  return PDFDocumentView;
  </code></pre>
  
  <p>When I've tweaked this back and forth I've managed to print the PDF but it's not properly aligned.</p>
  
  <p>About the alignment. I can see the red background I've created and I'm not able to see part of the right side of the pdf.</p>
  
  <p>So how do I best read and then print PDF files?</p>
  
  <p>Any ideas?</p>