【问题标题】:Compress an iOS PDF File?压缩 iOS PDF 文件?
【发布时间】:2013-04-15 17:28:22
【问题描述】:

我制作了一个可以制作 PDF 文件的应用程序。我只有一张图像作为基础,然后在其上叠加文本和其他图像以制作 PDF。问题是 PDF 大约 9 MB,只有一页。有没有办法将 PDF 压缩到不那么大?

- (IBAction)generatePdfButtonPressed:(id)sender
{
    pageSize = CGSizeMake(792, 612);
    NSString *fileName = @"Demo.pdf";
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];

    [self generatePdfWithFilePath:pdfFileName];
}

- (void) drawText
{
    CGContextRef    currentContext = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 0.0, 1.0);

    NSString *textToDraw = @"Text goes here";

    UIFont *font = [UIFont systemFontOfSize:22.0];



    CGRect renderingRect = CGRectMake(160, 177.82, 590, 95);

    [textToDraw drawInRect:renderingRect
                  withFont:font
             lineBreakMode:NSLineBreakByWordWrapping
                 alignment:NSTextAlignmentLeft];

}

- (void) drawImage
{
    UIImage * demoImage = [UIImage imageNamed:@"demo.png"];
    [demoImage drawInRect:CGRectMake( 0, 0, 792, 612)];
}

- (void) generatePdfWithFilePath: (NSString *)thefilePath
{
    UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);


    BOOL done = NO;
    do
    {

        UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);


         [self drawImage];

        [self drawText];


        done = YES;
    }
    while (!done);

    // Close the PDF context and write the contents out.
    UIGraphicsEndPDFContext();

}

【问题讨论】:

  • 这些图片有多大?你放了多少?
  • 如何将图像添加到 PDF 中?在将全尺寸图像添加到 PDF 但框架要小得多时,我遇到了类似的问题。 PDF 内部仍然包含完整尺寸的图像。我必须先将图像按比例缩放,然后将较小的图像添加到 PDF。
  • PDF 需要为 11" x 8.5"。背景图像为 11 x 8.5,大小为 13 MB。我尝试了一个较小的版本,但打印出来的 PDF 很模糊。查看原始问题以查看代码
  • demo.png 有多大(宽度和高度,以像素为单位)?如果它大于 792x612,那么您应该缩放图像(创建一个较小尺寸的新 UIImage)并将较小的 UIImage 添加到 PDF。
  • 它比 792 x 612 大,因为当我有较小的 792 x 612 图像时,它在 pdf 中变得模糊

标签: ios pdf compression


【解决方案1】:

当 PDF 包含图像时使 PDF 变小的方法是使用该图像的 JPEG 表示。 PDF 的大小取决于 JPEG 的压缩程度。我知道这一点不是通过在 iOS 中制作 PDF,而是通过在现实生活中制作 PDF。

这个问题可能对您有所帮助:Can I use JPEG compression for images in pdf files generated on iOS?

注意 ImageIO 框架的使用,以便您可以设置压缩级别。当然,您需要进行试验以获得最佳折衷值。

【讨论】:

  • 我不确定你的意思。我是否将demo.png保留在项目中,并在应用程序中对其进行压缩,然后将那个图像用于PDF?
【解决方案2】:

pod 'PDFGenerator', '~> 3.1' 做{

            let data = try PDFGenerator.generated(by: images, dpi: .default)
            
            debugPrint("--DataLengh: \(data.count / 1024)")
            
            if let doc = PDFDocument(data: data) {
                DispatchQueue.main.async {
                    completion(doc)
                }
            }
            
        } catch  {
            print("Func_createPdfFrom: \(error.localizedDescription)")
        }

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。
猜你喜欢
  • 2010-12-19
  • 2017-09-29
  • 2017-01-02
  • 2013-08-16
  • 1970-01-01
  • 1970-01-01
  • 2015-03-19
  • 1970-01-01
  • 2012-05-19
相关资源
最近更新 更多