Duck
Duck

Reputation: 36013

How to generate a PDF that must print at a specific size in inches?

I have a picture taken by a camera and this picture is 720x720 pixels. I need to generate a PDF that contains this picture. When I print this PDF at 600 dpi I need the picture to be exactly 2x2 inches (no problem if the image has to scale up a bit).

I have created the PDF context and write to it like this:

CGFloat ratio = 600.0f/72.0f; 
CGFloat contextWidth = floorf(imageWidth * ratio);
CGFloat contextHeight  = floorf(imageHeight * ratio);


CGSize PDFContextSize = CGSizeMake(contextWidth, contextHeight);

CGRect mediaBox = CGRectZero;
  mediaBox.size = PDFContextSize;


CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, &mediaBox, auxillaryInformation);

CGImageRef imageRef = [image CGImage];

CGPDFContextBeginPage(pdfContext, NULL);
CGContextDrawImage(pdfContext, CGRectMake(0.0f, 0.0f, mediaBox.size.width, mediaBox.size.height), imageRef);
CGPDFContextEndPage(pdfContext);
CGPDFContextClose(pdfContext);
CGContextRelease(pdfContext);

When I print the PDF created by this code at 600 dpi the printed image has 2.4 x 2.4 inches , instead of 2 x 2 inches.

Any ideas?

EDIT : This is the code I am using for printing. Yes, I am setting the scale factor to 1, to prevent the picture from scaling it.

  PDFDocument *pdfDoc = [[PDFDocument alloc] initWithData:pdfData];

  NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
  NSRect printRect = [printInfo imageablePageBounds];


  PDFPage *firstPage = [pdfDoc pageAtIndex:0];
  NSRect bounds = [firstPage boundsForBox:kPDFDisplayBoxMediaBox];
  NSSize pixelSize = bounds.size;


  NSRect frame = NSMakeRect(0.0f, 0.0f, 0.0f, 0.0f);
  frame.size = pixelSize;

  PDFView *pdfView = [[PDFView alloc] initWithFrame:frame];
  [pdfView setScaleFactor:1.0f];
  [pdfView setDocument: pdfDoc];

  [printInfo setTopMargin:verticalMargin];
  [printInfo setBottomMargin:verticalMargin];
  [printInfo setLeftMargin:horizontalMargin];
  [printInfo setRightMargin:horizontalMargin];
  [printInfo setHorizontalPagination:NSFitPagination];
  [printInfo setVerticalPagination:NSFitPagination];
  [printInfo setVerticallyCentered:YES];
  [printInfo setHorizontallyCentered:YES];
  [printInfo setScalingFactor:1.0f];

  NSPrintOperation *myop = [NSPrintOperation printOperationWithView:pdfView printInfo:printInfo];
  [myop runOperation];

the image printed by this code is 2.4x2.4 inches instead of 2x2 inches.

Upvotes: 1

Views: 817

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77606

A PDF that measures 2 x 2 inches is a PDF that measures 144 by 144 user units (assuming that the default value for the UserUnit entry was used).

In your Math, a 720 x 720 image isn't 144 x 144 user units:

720 * 600 / 72 = 6000
6000 / 72 = 83.333

You are mixing all kinds of measurements. By default, user units are the equivalent of points: 1 inch = 72 user units = 72 points. But you also involve pixels and pixels aren't points: Convert Pixels to Points

points = pixels * 72 / 96

Also you mention that you "print", but printing often changes the resolution if you forget to set the scaling to no scaling. See How can I override PDF print dialog settings with iTextSharp and also Chrome: How to print PDF with original size (100%, no scaling/shrinking)

In short: you are making several assumptions that may not be correct, such as the assumption that pixels are identical to user units, and the assumptions that printers respect the dimensions of a PDF document. Please double-check those assumptions, and if the problem persists, clarify your question.

Upvotes: 2

Related Questions