Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I use UIMarkupTextPrintFormatter to print a local html with local images?

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.

like image 483
Slee Avatar asked Aug 15 '11 18:08

Slee


4 Answers

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) {
        //
    }];

}
like image 171
Obliquely Avatar answered Nov 20 '22 13:11

Obliquely


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).

like image 35
Leon Deriglazov Avatar answered Nov 20 '22 15:11

Leon Deriglazov


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

like image 4
Quoc Anh Tran Avatar answered Nov 20 '22 14:11

Quoc Anh Tran


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!

like image 2
Slee Avatar answered Nov 20 '22 13:11

Slee