如何在 UIMarkupTextPrintFormatter 中添加图像?

发布于 2024-11-30 02:06:11 字数 115 浏览 5 评论 0原文

我正在尝试使用直接 HTML 打印文件,但是,在将图像添加到打印文件中时遇到困难。

如何在我要打印的 HTML 中引用项目中的图像? UIMarkupTextPrintFormatter 支持标签吗?

I'm trying to print a file using straight HTML, however, I am having difficulties adding images to the print file.

How can I reference images in my project in the HTML that I want to print? Does the UIMarkupTextPrintFormatter support tags?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如歌彻婉言 2024-12-07 02:06:11

它实际上比我想象的要简单得多:

NSString *yourImagePath = [[[NSBundle mainBundle] URLForResource:@"resource" withExtension:@"extension"] absoluteString];

然后你可以将图像路径放在 中,它就会渲染图像!瞧!

It's actually much simpler than I thought:

NSString *yourImagePath = [[[NSBundle mainBundle] URLForResource:@"resource" withExtension:@"extension"] absoluteString];

Then you can put the image path in an <img> and it will render the image! Voila!

梅窗月明清似水 2024-12-07 02:06:11

我一整天都在做这个,无论我如何将图像合并到我的打印作业中,它似乎总是无法渲染它们。我尝试了多种方法。 1) Base64 嵌入图像数据,2) 文件路径 URL 例如。 “file:///localhost/...”, 3) 基于字符串的文件路径“/Applications/...”。我一直将图像存储在我的沙箱文档目录中,但没有渲染出任何内容。 pdf中的其他内容都很好。我也尝试了上述方法,但没有成功,甚至连捆绑包中的图像都无法渲染出来。什么给?我什至将 html 放入文件中并在我的计算机上的 safari 中加载(因为在模拟器中测试,绝对路径是相同的)并且它可以工作。

源代码

+ (NSData*)renderMarkup:(NSString*)markup
{
  UIMarkupTextPrintFormatter *formatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:markup];

  SBAnnotationPrintPageRenderer *renderer = [[SBAnnotationPrintPageRenderer alloc] init];
  [renderer addPrintFormatter:formatter startingAtPageAtIndex:0];
  renderer.footerHeight = 10;
  renderer.headerHeight = 10;

  NSMutableData * pdfData = [NSMutableData data];
  UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil);

  for (NSInteger i = 0; i < [renderer numberOfPages]; i++)
  {
    UIGraphicsBeginPDFPage();

    CGRect bounds = UIGraphicsGetPDFContextBounds();
    [renderer drawPageAtIndex:i inRect:bounds];
  }

  UIGraphicsEndPDFContext();

  return pdfData;
}

和生成的 Html

<table>
    <tr>
        <td>
            <img style='background-color:blue;' src='/Users/zachthayer/Library/Application Support/iPhone Simulator/6.1/Applications/A47C1BDF-DCD1-49A5-A452-59802AEC2A94/Documents/29161AFF-BE35-4EEE-A7A1-ACF2DA9ABA71.png'/>
        </td>
        <td>
            <h2>Superadmin</h2>
            <i>00:00:00:00</i>
            <p>Test note</p>
        </td>
    </tr>
</table>

以及渲染的 PDF

渲染的 PDF

在开发计算机上的 safari 中渲染的相同 html

Safari 渲染

I have been at this all day, and no matter how I incorporate images into my print jobs, it always seems to not render them. I have tried multiple methods. 1) Base64 embedding image data, 2) Filepath URL eg. "file:///localhost/...", 3) String based file paths "/Applications/....". I have been storing the images in my sandbox Documents directory and nothing has rendered out. Everything else in the pdf is fine. I also attempted the above and to no avail not even images from the bundle will render out. What gives? I even out put the html to a file and loaded up in safari on my computer (since testing in simulator the absolute paths are the same) and it works.

Source

+ (NSData*)renderMarkup:(NSString*)markup
{
  UIMarkupTextPrintFormatter *formatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:markup];

  SBAnnotationPrintPageRenderer *renderer = [[SBAnnotationPrintPageRenderer alloc] init];
  [renderer addPrintFormatter:formatter startingAtPageAtIndex:0];
  renderer.footerHeight = 10;
  renderer.headerHeight = 10;

  NSMutableData * pdfData = [NSMutableData data];
  UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil);

  for (NSInteger i = 0; i < [renderer numberOfPages]; i++)
  {
    UIGraphicsBeginPDFPage();

    CGRect bounds = UIGraphicsGetPDFContextBounds();
    [renderer drawPageAtIndex:i inRect:bounds];
  }

  UIGraphicsEndPDFContext();

  return pdfData;
}

And the generated Html

<table>
    <tr>
        <td>
            <img style='background-color:blue;' src='/Users/zachthayer/Library/Application Support/iPhone Simulator/6.1/Applications/A47C1BDF-DCD1-49A5-A452-59802AEC2A94/Documents/29161AFF-BE35-4EEE-A7A1-ACF2DA9ABA71.png'/>
        </td>
        <td>
            <h2>Superadmin</h2>
            <i>00:00:00:00</i>
            <p>Test note</p>
        </td>
    </tr>
</table>

And the Rendered PDF

Rendered PDF

Same html rendered in safari on the development computer

Safari render

过去的过去 2024-12-07 02:06:11

本地图像应该首先放置在 imageView 中,然后转换为 base64 值并在 html 中的图像标签中使用它。

let imageData:NSData = UIImagePNGRepresentation(imageView.image!)!
let strBase64:String = imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
var imageTag = String(format: "<img src=\"data:image/png;base64,%@\"", strBase64)

Locally Images should be first placed inside a imageView and then converted to base64 value and use it inside the image tag in html.

let imageData:NSData = UIImagePNGRepresentation(imageView.image!)!
let strBase64:String = imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
var imageTag = String(format: "<img src=\"data:image/png;base64,%@\"", strBase64)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文