【问题标题】:How to generate a PDF that must print at a specific size in inches?如何生成必须以特定尺寸(以英寸为单位)打印的 PDF?
【发布时间】:2017-02-19 09:55:22
【问题描述】:

我有一张相机拍摄的照片,这张照片是 720x720 像素。 我需要生成包含此图片的 PDF。 当我以 600 dpi 打印此 PDF 时,我需要图片正好是 2x2 英寸(如果图像必须放大一点也没问题)。

我已经创建了 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);

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

有什么想法吗?

编辑:这是我用于打印的代码。是的,我将比例因子设置为 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];

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

【问题讨论】:

    标签: ios macos cocoa pdf-generation


    【解决方案1】:

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

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

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

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

    points = pixels * 72 / 96
    

    您还提到您“打印”,但如果您忘记将 scaling 设置为 no scaling,打印通常会更改分辨率。见How can I override PDF print dialog settings with iTextSharpChrome: How to print PDF with original size (100%, no scaling/shrinking)

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

    【讨论】:

    • OP 写道“我有一张由相机拍摄的照片,这张照片是 720x720 像素”——这些像素没有固有的“大小”。无论 OPs 的问题是什么,肯定不会将它们视为 Windows 默认的 1/96 英寸屏幕像素。
    • 我的答案不是确定的答案。我只是想指出一些事情。我无法解释为什么 720 x 720 像素的图像会生成 2.4 x 2.4 英寸的 PDF。我也不知道 OP 是在谈论打印尺寸(可能由打印机缩放)还是查看器报告的尺寸。
    • 我已经用我用来打印 PDF 的代码更新了问题。
    猜你喜欢
    • 2018-06-24
    • 2013-07-19
    • 2016-10-08
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多