如何生成必须以英寸特定尺寸打印的PDF?

时间:2021-06-29 21:10:39

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).

我有一张相机拍的照片,这张照片是720x720像素。我需要生成包含此图片的PDF。当我以600 dpi打印此PDF时,我需要将图片精确地设置为2x2英寸(如果图像必须稍微向上扩展则没问题)。

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

我创建了PDF上下文并像这样写入:

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.

当我以600 dpi打印由此代码创建的PDF时,打印的图像为2.4 x 2.4英寸,而不是2 x 2英寸。

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.

编辑:这是我用于打印的代码。是的,我将比例因子设置为1,以防止图片缩放。

  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.

此代码打印的图像为2.4x2.4英寸而不是2x2英寸。

1 个解决方案

#1


2  

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).

测量2 x 2英寸的PDF是一个PDF,用144个用户单位测量144(假设使用了UserUnit条目的默认值)。

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

在您的数学中,720 x 720图像不是144 x 144用户单位:

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

您正在混合各种测量。默认情况下,用户单位相当于点数:1英寸= 72个用户单位= 72点。但你也涉及像素和像素不是点:将像素转换为点

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)

您还提到“打印”,但如果您忘记将缩放设置为无缩放,则打印通常会更改分辨率。请参阅如何使用iTextSharp以及Chrome覆盖PDF打印对话框设置:如何以原始尺寸打印PDF(100%,无缩放/收缩)

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.

简而言之:您正在做出一些可能不正确的假设,例如假设像素与用户单位相同,以及打印机尊重PDF文档尺寸的假设。请仔细检查这些假设,如果问题仍然存在,请澄清您的问题。

#1


2  

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).

测量2 x 2英寸的PDF是一个PDF,用144个用户单位测量144(假设使用了UserUnit条目的默认值)。

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

在您的数学中,720 x 720图像不是144 x 144用户单位:

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

您正在混合各种测量。默认情况下,用户单位相当于点数:1英寸= 72个用户单位= 72点。但你也涉及像素和像素不是点:将像素转换为点

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)

您还提到“打印”,但如果您忘记将缩放设置为无缩放,则打印通常会更改分辨率。请参阅如何使用iTextSharp以及Chrome覆盖PDF打印对话框设置:如何以原始尺寸打印PDF(100%,无缩放/收缩)

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.

简而言之:您正在做出一些可能不正确的假设,例如假设像素与用户单位相同,以及打印机尊重PDF文档尺寸的假设。请仔细检查这些假设,如果问题仍然存在,请澄清您的问题。