6

Here is the string I want to print:

<p><img src="/Users/Max/Library/Application Support/iPhone Simulator/4.3/Applications/5FCCB847-52D9-48F4-A900-459C6A77A5A6/Documents/18/logo18_lg.jpg" alt="" height="72" /></p>

This is a small fragment of a larger HTML page I have generated in my application, and I am passing to this to the print like so:

UIMarkupTextPrintFormatter *html = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:printContents];

Where print contents contains all of the html I need to print including the image snippet above. The printing works great EXCEPT for the images not printing.

Slee
  • 27,498
  • 52
  • 145
  • 243

4 Answers4

6

I'd love to be contradicted, but I don't think you can get the approach you're looking at to work. A work around is to embed the image into the HTML itself. The following illustrates the principle (adapted from this forum post from 2005). You can create a string to represent your image on the fly. A good place to start might be this stackoverflow question.

- (void) buttonTapped;
{
    NSString* printContents = @"This is a work around <IMG SRC=\"data:image/gif;base64,R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAwAAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQu5yqmCYsapyuvUU lvONmOZtfzgFzByTB10QgxOR0TqBQejhRNzOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiOSpa/TPg7JpJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQ ZXZeYGejmJlZeGl9i2icVqaNVailT6F5iJ90m6mvuTS4OK05M0vDk0Q4XUtwv KOzrcd3iq9uisF81M1OIcR7lEewwcLp7tuNNkM3uNna3F2JQFo97Vriy/Xl4/f1cf5VWzXyym7PHhhx4dbgYKAAA7\">";

    UIMarkupTextPrintFormatter *html = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:printContents];
    UIPrintInteractionController* printController = [UIPrintInteractionController sharedPrintController];
    [printController setPrintFormatter:html];
    [printController presentAnimated:YES completionHandler:^(UIPrintInteractionController *printInteractionController, BOOL completed, NSError *error) {
        //
    }];

}
Community
  • 1
  • 1
Obliquely
  • 7,002
  • 2
  • 32
  • 51
4

It is possible to print HTML with images using UIWebView's viewPrintFormatter instead of configuring UIMarkupTextPrintFormatter with HTML content (which will only print text). You can load your content either from local or remote location and then initiate printing in your webViewDidFinishLoad: method implementation.

Sample Code is available from Apple (found in UIWebView class reference).

Leon Deriglazov
  • 1,132
  • 9
  • 13
4

SWIFT 3: It took me a while to figure this out, but if you use the prefix "file://" in front of your URL, then it'll work. Like so:

<p><img src="file:///Users/Max/Library/Application Support/iPhone Simulator/4.3/Applications/5FCCB847-52D9-48F4-A900-459C6A77A5A6/Documents/18/logo18_lg.jpg" alt="" height="72" /></p>

Hope this helps

  • 1
    Actually, you will also need to wait some time after calling UIMarkupTextPrintFormatter(). Rendering takes some time, so if you export to pdf right away, your images have not finished rendering and will not show in the pdf document. More on this here: https://stackoverflow.com/questions/40239515/uimarkuptextprintformatter-never-renders-base64-images – Quoc Anh Tran Jul 19 '17 at 05:29
  • this should be the accepted answer but with the edit that you have to wait a small amount of time for the UIMarkupTextPrintFormatter to finish rendering the images – christopher.online Oct 19 '17 at 15:26
2

Suggestion above works perfectly, here is what you need to do it:

download and add this to your project: https://github.com/mikeho/QSUtilities

load your file into NSData:

NSData *logo = [[NSData alloc] initWithContentsOfFile:filePath];

encode using QSStrings to get your encoding, don't forget to change the file type if it is a image/gif:

NSString *encodedJpg = [NSString stringWithFormat:@"data:image/jpg;base64,%@",[QSStrings encodeBase64WithData:logo]];

use the encodedJpg string in the img src like Matthew did above.

this solved a huge problem for me - thank you!

Slee
  • 27,498
  • 52
  • 145
  • 243