1

I have an HTML string (a series of nested tables) that I'm trying to print in iOS. Below is the method in my ViewController that initiates the printing:

- (IBAction) Print:(id)sender
{
    UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
    if(!controller){
        NSLog(@"Couldn't get shared UIPrintInteractionController!");
        return;
    }
    controller.printFormatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:@"<!DOCTYPE html PUBLIC\" -//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv =\"Content-Type\" content=\"text/html;charset=utf-8\"/><title>SpanCalc</title></head><body style=\"margin-right:5%;font-family:Arial, Helvetica\"><H3>SpanCalc</H3><span style=\"padding-bottom:10px\"><table style=\"border:2px solid;border-collapse:collapse\"><tr><td style=\"border:1px solid;padding-left:5px;padding-right:5px;\"><H3>Floor Layer</H3><span style=\"padding-bottom:10px\"><table style=\"border:2px solid;border-collapse:collapse\"><tr><td style=\"background-color:rgba(0,0,0,0.1);border-top:1px solid\"><table width=100% style=\"table-layout:fixed\"><tr><td>Floor Covering:</td></tr><tr><td align=\"right\" style=\"padding-right:5px;\">Cushioned Vinyl</td></tr></table></td></tr><tr><td style=\"background-color:rgba(0,0,0,0.1);border-top:1px solid\"><table width=100% style=\"table-layout:fixed\"><tr><td>Gypsym Concrete Topped:</td></tr><tr><td align=\"right\" style=\"padding-right:5px;\">False</td></tr></table></td></tr><tr><td style=\"background-color:rgba(0,0,0,0.1);border-top:1px solid\"><table width=100% style=\"table-layout:fixed\"><tr><td>Subfloor:</td></tr><tr><td align=\"right\" style=\"padding-right:5px;\">1 Layer ²³/₃₂\" OSB</td></tr></table></td></tr></table></span></td></tr><tr><td style=\"border:1px solid;padding-left:5px;padding-right:5px;\"><H3>Framing</H3><span style=\"padding-bottom:10px\"><table style=\"border:2px solid;border-collapse:collapse\"><tr><td style=\"background-color:rgba(0,0,0,0.1);border-top:1px solid\"><table width=100% style=\"table-layout:fixed\"><tr><td>Framing:</td></tr><tr><td align=\"right\" style=\"padding-right:5px;\">I-Joists</td></tr></table></td></tr><tr><td style=\"background-color:rgba(0,0,0,0.1);border-top:1px solid\"><table width=100% style=\"table-layout:fixed\"><tr><td>On Center Spacing:</td></tr><tr><td align=\"right\" style=\"padding-right:5px;\">@24\"o.c.</td></tr></table></td></tr><tr><td style=\"background-color:rgba(0,0,0,0.1);border-top:1px solid\"><table width=100% style=\"table-layout:fixed\"><tr><td>Joist Depth:</td></tr><tr><td align=\"right\" style=\"padding-right:5px;\">&lt; 14\" I-joists</td></tr></table></td></tr></table></span></td></tr><tr><td style=\"border:1px solid;padding-left:5px;padding-right:5px;\"><H3>Ceiling Layer</H3><span style=\"padding-bottom:10px\"><table style=\"border:2px solid;border-collapse:collapse\"><tr><td style=\"background-color:rgba(0,0,0,0.1);border-top:1px solid\"><table width=100% style=\"table-layout:fixed\"><tr><td>Subfloor:</td></tr><tr><td align=\"right\" style=\"padding-right:5px;\">2 Layers ¹/₂\" GWB</td></tr></table></td></tr><tr><td style=\"background-color:rgba(0,0,0,0.1);border-top:1px solid\"><table width=100% style=\"table-layout:fixed\"><tr><td>Resilient Channel Spacing:</td></tr><tr><td align=\"right\" style=\"padding-right:5px;\">@16\"o.c.</td></tr></table></td></tr><tr><td style=\"background-color:rgba(0,0,0,0.1);border-top:1px solid\"><table width=100% style=\"table-layout:fixed\"><tr><td>Insulation:</td></tr><tr><td align=\"right\" style=\"padding-right:5px;\">3.5\" Fiberglass Batt</td></tr></table></td></tr></table></span></td></tr></table></span></body></html>"];

    UIPrintInteractionCompletionHandler completionHandler =
    ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
        if(!completed && error){
            NSLog(@"FAILED! due to error in domain %@ with error code %ld", error.domain, (long)error.code);
        }
    };

    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.outputType = UIPrintInfoOutputGeneral;
    printInfo.jobName = @"PRINT TEST";
    printInfo.duplex = UIPrintInfoDuplexNone;
    controller.printInfo = printInfo;
    [controller presentAnimated:YES completionHandler:completionHandler];  // iPhone   
}

An objective-c demo project can be found here: https://github.com/baskren/PrintTest. The HTML should fit on one Portrait oriented page (I measured it). However, UIPrintInteractionController is breaking the content into multiple pages:

enter image description here

Things I have tried, unsuccessfully, to address this:

  • Embed my UIMarkupTextPrintFormatter in a UIPrintRenderer;
  • Set the printFormatter.MaximumContentHeight (makes things worse);
  • Use a print formatter from a UIWebView;
  • Set the printFormatter.PerPageContentInsets
  • Set the printFormatter.ContentInsets
  • Set the printFormatter.StartPage

Note that this issue does not appear to happen if printInfo.Orientation is set to Landscape.

Any insights would be most appreciated!

baskren
  • 963
  • 8
  • 19
  • Have you tried to convert the page into a UIImage and then you can resize the image the way you want for you UIPrintInteractionController? https://stackoverflow.com/questions/10231145/resize-uiimage-for-uiprintinteractioncontroller – Saamer Dec 19 '19 at 00:16
  • I've tried avoiding that approach primarily because Apple's WkWebView.ViewPrintFormatter appears to respect the ```page-break-before: always``` style ... which is significant in preventing images and charts from being split between pages. – baskren Dec 23 '19 at 12:39
  • Hello. I think you have a lot of backslash which can cause line breaks if you missed one. Try with a single quote for the string and remove all the backslashes. Try and tell me – Yona Smilevitch Dec 23 '19 at 15:45
  • Thanks for the suggestion. Not a bad idea. I just tried it (by replacing all `\"` with `'`) to no avail. bummer. – baskren Dec 24 '19 at 21:22
  • FWIW, I also just tried adding `page-break-inside:avoid` to the `
    – baskren Dec 26 '19 at 12:27

0 Answers0