How to Add Images in UIMarkupTextPrintFormatter?
Asked Answered
G

3

14

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?

Gurgle answered 14/8, 2011 at 17:43 Comment(0)
G
11

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!

Gurgle answered 16/8, 2011 at 13:11 Comment(4)
That doesn't seem to work for me. I do something like this: <img src="file:///Users/xxx/Library/Developer/CoreSimulator/Devices/805D90F9-66E9-4CE0-BE9F-6E2085655CF1/data/Containers/Bundle/Application/19F746F2-0691-4BEB-AF99-BEB2E6D5BFE9/CreatePDF.app/image002.png">Consolatory
It works for me when fed to UIPrintInteractionController, but not when I try to create PDF file with UIGraphicsBeginPDFContextToDataBeatrizbeattie
It does not work for me using UIGraphicsBeginPDFContextToFileEruption
What about images that are not bundled but downloaded instead?Prowl
C
5

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

Catalog answered 24/4, 2013 at 0:57 Comment(0)
W
1

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)
Whippersnapper answered 13/8, 2016 at 6:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.