Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong><em>Update: See Edit 3 for an answer to op's original question</em></strong></p> <p>There are two options:</p> <ol> <li><p>Use a library to convert the HTML to PDF. This is by far the best option, since it will (probably) preserve text as vectors.</p></li> <li><p>Get a high resolution render of the HTML and save it as a PNG (not PDF surely!).</p></li> </ol> <p>For HTML to PDF, <a href="http://code.google.com/p/wkhtmltopdf/" rel="noreferrer">wkhtmltopdf</a> looks like a good option, but it relies on Qt which you can't really use on Android. There are some other libraries but I doubt they do the PDF rendering very well.</p> <p>For getting a high-res webview, you could try creating your own <code>WebView</code> and calling <code>onMeasure(...)</code> and <code>onLayout(...)</code> and pass appropriate parameters so the view is really big. Then call <code>onDraw(myOwnCanvas)</code> and the webview will draw itself to your canvas, which can be backed by a <code>Bitmap</code> using <code>Canvas.setBitmap()</code>.</p> <p>You can probably copy the state into the new <code>WebView</code> using something like</p> <pre><code>screenshotterWebview.onRestoreInstanceState(mWebView.onSaveInstanceState()); </code></pre> <p>Orrr it may even be possible to use the same <code>WebView</code>, just temporarily resize it to be large, <code>onDraw()</code> it to your canvas, and resize it back again. That's getting very hacky though! </p> <p>You might run into memory issues if you make it too big.</p> <h1>Edit 1</h1> <p>I thought of a third, exactly-what-you-want option, but it's kind of hardcore. You can create a custom <code>Canvas</code>, that writes to a PDF. In fact, it is <em>almost</em> easy, because underlying <code>Canvas</code> is Skia, which actually includes a PDF backend. Unfortunately you don't get access to it on Android, so you'll basically have to build your own copy of it on Android (there are instructions), and duplicate/override all the <code>Canvas</code> methods to point to your Skia instead of Androids. Note that there is a tempting <code>Picture.writeToStream()</code> method which serializes the Skia data, but unfortunately this format is not forwards or backwards compatible so if you use it your code will probably only work on a few versions of Android.</p> <p>I'll update if/when I have fully working code.</p> <h1>Edit 2</h1> <p>Actually it is impossible to make your own "intercepting" <code>Canvas</code>. I started doing it and went through the tedious process of serializing all function calls. A few you can't do because they are hidden, but those didn't look important. But right at the end I came to serializing <code>Path</code> only to discover that it is write-only. That seems like a killer to me, so the only option is to interpret the result of <code>Picture.writeToStream()</code>. Fortunately there are only two versions of that format in use, and they are nearly identical.</p> <h1>Edit 3 - Really simple way to get a high resolution <code>Bitmap</code> of a view</h1> <p>Ok, it turns out just getting a high res bitmap of a view (which can be the entire app) is trivial. Here is how to get double resolution. Obviously all the bitmaps look a bit crap, but the text is rendered at full resolution:</p> <pre><code>View window = activity.getWindow().getDecorView() Canvas bitmapCanvas = new Canvas(); Bitmap bitmap = Bitmap.createBitmap(window.getWidth()*2, window.getHeight()*2, Bitmap.Config.ARGB_8888); bitmapCanvas.setBitmap(bitmap); bitmapCanvas.scale(2.0f, 2.0f); window.draw(bitmapCanvas); bitmap.compress(Bitmap.CompressFormat.PNG, 0, myOutputStream); </code></pre> <p>Works like a charm. I've now given up on getting a PDF screenshot with vector text. It's certainly possible, but very difficult. Instead I am working on getting a high-res PSD where each draw operation is a separate layer, which should be much easier.</p> <h1>Edit 4</h1> <p>Woa this is getting a bit long, but success! I've generated an <code>.xcf</code> (GIMP) and PDF where each layer is a different canvas drawing operation. It's not quite as fine-grained as I was expecting, but still, pretty useful!</p> <p>Actually my code just outputs full-size PNGs and I used "Open as layers..." and "Autocrop layer" in GIMP to make these files, but of course you can do that in code if you like. I think I will turn this into a blog post.</p> <p>Download the <a href="http://concentriclivers.com/misc/android_layout_test.xcf" rel="noreferrer">GIMP</a> or <a href="http://concentriclivers.com/misc/android_layout_test.xcf" rel="noreferrer">Photoshop</a> demo file (rendered at 3x resolution).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload