【问题标题】:iOS render "Hello, world" (Helv.Neue Light, 180pt) as a PDF?iOS 将“Hello, world”(Helv.Neue Light,180pt)渲染为 PDF?
【发布时间】:2014-06-29 21:53:14
【问题描述】:

我想在 UIKit 中动态生成一个最小的 PDF 页面,

所以直接发送到 AirPrint(不涉及文件),

仅包含一行文本,例如,

你好,世界

作为 Helvetica Neue Light 180pt.

TBC 它必须在 PDF 中实际排版,呈现为图像。

请注意,渲染位图的代码很简单,而且可以广泛使用。示例https://stackoverflow.com/a/6566696/294884

我已经阅读并尝试过,直到我脸色发青。任何人都可以这样做吗?

PS如果你正在阅读这篇文章并且你是一名 Postscript 程序员,那么我专门讨论的是 iPad 中的 PDF 生成系统,例如:https://developer.apple.com/library/mac/documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_pdf/dq_pdf.html

(我完全不清楚 Quartz 是否是最好的方法——这是一场噩梦。)

顺便说一下,这是使用 html 方法执行此操作的确切代码...

此示例将一些动态 html 直接发送到 AirPrint,不涉及任何文件

PDF 比较复杂

-(NSString*)_sample
  {
  NSString *r = @"<table border=0 cellpadding=0 cellspacing=0 width=100%>"
        "<tr><td align=right>"
        "<font face=courier size=1>Almost a header!</font>"
        "</tr></td></table>"
        "<br><br><br><br>"

        "<font size=4 face=sans-serif>"
        "Hello"
        "<br><br><br><br>"
        "Some <i>italics</i>."
        "<br><br><br><br>"

        "<table border=1 cellpadding=10 cellspacing=1>"
        "<tr><td align=right>one</td><td>two</td><td>three</td></tr>"
        "</table>"

        "</font>";
  return r;
  }

-(IBAction)printRawHtml:(UIView *)sender
  {
  UIPrintInfo *pif = [UIPrintInfo printInfo];
  pif.outputType = UIPrintInfoOutputGeneral;
  pif.jobName = @"Test HTML-like Job";

  UIMarkupTextPrintFormatter *formatter = [[UIMarkupTextPrintFormatter alloc]
    initWithMarkupText:[self _sample]
    ];

  formatter.startPage = 0;
  formatter.contentInsets = UIEdgeInsetsMake(10, 72.0, 72.0, 72.0);
                               //top,left,bottom,right

  UIPrintInteractionController *pic =
      [UIPrintInteractionController sharedPrintController];
  pic.delegate = self;
  pic.printInfo = pif;
  pic.printFormatter = formatter;
  pic.showsPageRange = NO;
  pic.showsNumberOfCopies = NO;

  void (^cph)(UIPrintInteractionController *, BOOL, NSError *) =
    ^(UIPrintInteractionController
          *printController, BOOL completed, NSError *error)
      {
      if (!completed && error) NSLog(@"print error %@", error);
      };

  if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    [pic presentFromRect:sender.frame
       inView:self.view animated:YES completionHandler:cph];
  else
    [pic presentAnimated:YES completionHandler:cph];
  }

【问题讨论】:

  • 为什么是标签 [ios]?为什么[后记]? (如果你想要“任何”解决方案,我可以用原始 PostScript 打高尔夫球!)
  • PS 谁在这里添加了“太宽泛”的标志,是不连贯的。你知道“太多方法”来做到这一点吗?!好伤心。请告诉我一两个:)
  • 您拥有的链接显示了如何将 pdf 作为输出设备打开。只需使用普通的 Quartz 文本工具(我对此一无所知)就可以了。假设链接文档中的所有内容都是正确的(可能是),如果您创建了 PDF 图形上下文,那么您在该上下文中的所有绘图(和文本设置)都将进入 pdf。我对该软件知之甚少,无法保证您的文本将保持为文本而不被光栅化。但它可能会,因为这就是 pdf 的重点。
  • 您可能还需要在 pdf 中嵌入字体(或仅包含您使用的字形的子集)。

标签: ios pdf postscript


【解决方案1】:

来自 Apple 开发者网站的代码

- (IBAction)savePDFFile:(id)sender
{
    // Prepare the text using a Core Text Framesetter.
    CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, (CFStringRef)textView.text, NULL);
    if (currentText) {
        CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);
        if (framesetter) {

            NSString *pdfFileName = [self getPDFFileName];
            // Create the PDF context using the default page size of 612 x 792.
            UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);

            CFRange currentRange = CFRangeMake(0, 0);
            NSInteger currentPage = 0;
            BOOL done = NO;

            do {
                // Mark the beginning of a new page.
                UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);

                // Draw a page number at the bottom of each page.
                currentPage++;
                [self drawPageNumber:currentPage];

                // Render the current page and update the current range to
                // point to the beginning of the next page.
                currentRange = [self renderPageWithTextRange:currentRange andFramesetter:framesetter];

                // If we're at the end of the text, exit the loop.
                if (currentRange.location == CFAttributedStringGetLength((CFAttributedStringRef)currentText))
                    done = YES;
            } while (!done);

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

            // Release the framewetter.
            CFRelease(framesetter);

        } else {
            NSLog(@"Could not create the framesetter needed to lay out the atrributed string.");
        }
        // Release the attributed string.
        CFRelease(currentText);
    } else {
            NSLog(@"Could not create the attributed string for the framesetter");
    }
}

这是来自 Apple 的官方文档

https://developer.apple.com/library/ios/documentation/2ddrawing/conceptual/drawingprintingios/GeneratingPDF/GeneratingPDF.html

【讨论】:

    【解决方案2】:

    就像@luser droog 提到的,您可以使用 Quartz2D 在 iOS 中生成 PDF。我自己从未尝试过,但您可以查看this tutorial by Ray Wenderlich。他的教程特别详细介绍了如何以编程方式创建 PDF 以显示简单的“Hello world”字符串。

    【讨论】:

    • 嗯 - 好主意!这是一个很好的链接。它专注于写入文件,但也许这是唯一的方法......
    • 我相信它也会用于显示它...我不确定从长远来看你想用它做什么,但是一旦它显示你也可以随时查看 AirPrint如果你需要用它做任何其他事情。或者只是使用那个小小的“分享”按钮将其作为附件添加到电子邮件等中。
    • 当然,重点是直接使用 AirPrint - 我会在问题中澄清!再次伟大的链接谢谢!我只是没想到那里看
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    • 2013-07-20
    • 1970-01-01
    • 2014-12-07
    • 2018-02-16
    相关资源
    最近更新 更多