iOS Question Canvas.DrawView(WebView) does not print images

MitchBu

Well-Known Member
Licensed User
To convert html to pdf in modern ios releases is relatively simple. Which minimum ios release do you want to support ?

I don't know yet. It will be my first B4i app.

I don't even know how to specify it in Project attributes :)

I had a look at the stats below; seems anything below 10.3 is pretty anecdotal.

So which minimum would you suggest to convert to PDF ?
 
Upvote 0

MitchBu

Well-Known Member
Licensed User
Thank you Semen.

But I get an error:


B4i Version: 7.20
Parsing code. (0.00s)
Building folders structure. (0.02s)
Compiling code. (0.01s)
Compiling layouts code. (0.01s)
Compiling debugger engine code. (0.44s)
Building Xcode project. (0.27s)
Preparing project for builder. Error
File not found: C:\Users\mitch\Documents\Matchfonts\1 - Development\B4i\keys\XCodeFireBase.mobileprovision
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
It's possible to convert each PDF page to image using even UIKit only. Sure, I saw similar fragments.
But, first of all, it's necessary to understand, what do you actually need.

If a webview does not have a scrollbar and you need a simple copy of webview's visible context, a solution is enough simple and requires less than 10 statements.
For example, add to the bottom
B4X:
#If OBJC
#import <Webkit/Webkit.h>
- (UIImage *) createImage: (WKWebView *) wkWebView
   {
   UIGraphicsBeginImageContext (CGSizeMake (wkWebView.layer.frame.size.width, wkWebView.layer.frame.size.height));
   [wkWebView.layer renderInContext: UIGraphicsGetCurrentContext ()];
   UIImage *image = UIGraphicsGetImageFromCurrentImageContext ();
   UIGraphicsEndImageContext ();
   return image;
   }
#End If
In WebView1_PageFinished event
B4X:
Dim no As NativeObject = Me
Dim bitmap As Bitmap = no.RunMethod ("createImage:", Array (WebView1))
You can show bitmap in imageview or to save it
B4X:
Dim outputStream As OutputStream = File.OpenOutput (File.DirDocuments, "tmp.jpg", False)
bitmap.WriteToStream (outputStream, 100, "JPEG")
outputStream.Close

But if the size of webview is not enough to show a whole document, I think, PDF format is better.
 
Last edited:
Upvote 0

MitchBu

Well-Known Member
Licensed User
It's possible to convert each PDF page to image using even UIKit only. Sure, I saw similar fragments.
But, first of all, it's necessary to understand, what do you actually need.

If a webview does not have a scrollbar and you need a simple copy of webview's visible context, a solution is enough simple and requires less than 10 statements.

Hi Semen,

I appreciate that code.

However, pictures from the HTML page are missing from tmp.jpg.

Perhaps converting tmp.pdf to bitmap would be better ?

What I am doing is printing both a check, and a letter, on the same letter size sheet.

The check is printed on a PDF from the data entered by the user, through a series of DrawText and DrawPicture.

Then I print the content of the letter as bitmap, which was created as HTML, and displays in a WebView.

Thank you Semen :)
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
Well, I will search a fragment to convert pdf to a set of jpg

B4X:
However, pictures from the HTML page are missing from tmp.jpg.

Unlike I saw discussions about images, I tried to output jpg for https://cnn.com and it looks correct.
Probably there are troubles with local pictures

tmp.jpg
I
 
Upvote 0

MitchBu

Well-Known Member
Licensed User
Well, I will search a fragment to convert pdf to a set of jpg

B4X:
However, pictures from the HTML page are missing from tmp.jpg.

Unlike I saw discussions about images, I tried to output jpg for https://cnn.com and it looks correct.
Probably there are troubles with local pictures

You are right. It may have to do with local files.

Thank you.
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
I added convertPdfPageToImage function. So, you can call createPDF function and then (in cycle) convertPdfPageToImage, which returns a bitmap for certain page. createImage is not needed.

If you want to change paper size, correct kPaperSizeA4. Letter format is 8.5 x 11 inches. Means width = 8.5 * 72 = 612, height = 11 * 72 = 792, Also you may want to correct offsets (topPadding ...).

A program sends e-mail. Before exit, wait "MessageSent" message.

Probably, it's possible to increase a quality of pdf inside createPDF (similar like inside convertPdfPageToImage function). I will try somehow.
 

Attachments

  • s19.zip
    6.3 KB · Views: 44
Upvote 0

MitchBu

Well-Known Member
Licensed User
I added convertPdfPageToImage function. So, you can call createPDF function and then (in cycle) convertPdfPageToImage, which returns a bitmap for certain page. createImage is not needed.

@Semen Matusovskiy , you are the best !

Now the bitmap sports pictures, and I am able to print the letter together with the check :)

Thank you SO MUCH :)
 
Upvote 0
Top