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.
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) {
//
}];
}
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).
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
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With