iOS Question Webview.PrintHTML in B4i

StephanP82

Member
Licensed User
Do exists an real alternative for Webview.PrintHTML in B4i?

I've tried with Canvas.InitializePDF and then draw the Webview to the file but depending on the screen resolution of the device I had to zoom the Webview to get all of its content visible on the drawed screenshot. So I'm unable to test and set a zoom level for every screen size because I don't have a real iOS device and the simulator don't returns the real screen sizes.

Any ideas for a better solution?
 

StephanP82

Member
Licensed User
It's a self written HTML form containing a large table and texts. I've choosen a HTML file instead of a B4X Table because it's needed to zoom and to merge some cells.

Here's what i actually do:
B4X:
        Dim oCanvas As Canvas
        Dim Rect As Rect

        PrintMode = True
        SaveSettingsToDB(True)
        PaintHtml(SelectedMonth, SelectedYear) '<- that builds the html string a load it to the Webview

        Rect.Initialize(10, 20, 595, 842)
        oCanvas.InitializePDF(File.DirDocuments, FileName, 595, 842)
        oCanvas.DrawView(WW_Webview, Rect)
        oCanvas.Release
                
        DocInteraction.Initialize("DocInteraction", File.DirDocuments, FileName)
        DocInteraction.PreviewFile(Page1)

It views the document in the preview but its only a part of the html table. So I've tried to zoom out the webview before to get all the content visible but depending on the screen resolution of the device many different zoom levels were needed. My next try will be to change the HTML code at PrintMode to fill the HTML table to the height of the screen.

It's just a bad workarround. I only want to get the HTML table to the preview and to print it in full A4 size.
 
Upvote 0

b4x-de

Member
Licensed User
Hi Stephan, did you find a way to style the document for printing? I am currently facing the same problem and would be interested in your solution. Thanks, Thomas
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
There are some examples on stackoverflow.
For example, add to the bottom of Main following code
B4X:
#If OBJC
#define kPaperSizeA4 CGSizeMake (595.2, 841.8)
- (void) createPDF: (UIWebView *) wkWebView : (NSString *) path
    {
    UIPrintPageRenderer * render = [[UIPrintPageRenderer alloc] init];
    [render addPrintFormatter: wkWebView.viewPrintFormatter startingAtPageAtIndex: 0];
    // set these values according to your requirement
    float topPadding    = 72.0f;
    float bottomPadding = 42.0f;
    float leftPadding   = 72.0f;
    float rightPadding  = 42.0f;
    CGRect printableRect = CGRectMake (leftPadding, topPadding, kPaperSizeA4.width - leftPadding - rightPadding, kPaperSizeA4.height - topPadding - bottomPadding);
    CGRect paperRect = CGRectMake (0, 0, kPaperSizeA4.width, kPaperSizeA4.height);
    [render setValue: [NSValue valueWithCGRect:paperRect] forKey: @"paperRect"];
    [render setValue: [NSValue valueWithCGRect:printableRect] forKey: @"printableRect"];
    NSMutableData *pdfData = [NSMutableData data];
    UIGraphicsBeginPDFContextToData (pdfData, paperRect, nil );
    [render prepareForDrawingPages: NSMakeRange (0, render.numberOfPages)];
    CGRect bounds = UIGraphicsGetPDFContextBounds ();
    for (int i = 0 ; i < render.numberOfPages; i++)
        {
        UIGraphicsBeginPDFPage ();
        [render drawPageAtIndex: i inRect: bounds];
        }
    UIGraphicsEndPDFContext();   
    if (pdfData) { [pdfData writeToFile: path atomically: YES]; } else { NSLog(@"PDF could not be created"); }
    }
#End If

After WebView1_PageFinished event call this subroutine:
B4X:
Dim no As NativeObject = Me
no.RunMethod ("createPDF::", Array (WebView1, File.Combine (File.DirDocuments, "tmp.pdf")))

This code assumes A4 with 1 dime padding from left and top, 0,6 dime from right and bottom. Of course, you can change paper size and padding.

A code is very old (written 5-6 years ago). And it works up to now (I tested in IOS 14 simulator).

I attached a PDF, created from this page. I sent it to myself using SMTP object (iNet library).
 

Attachments

  • tmp.pdf
    82.3 KB · Views: 47
Last edited:
Upvote 0
Top