So I am trying to create a PDF from a Webview. Right now I can create an image from the webview, but I am having some problems to split my document in many pages.
First, I create a Bitmap from the webview:
public static Bitmap screenShot(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return bitmap;
}
Second, I create and I show the PDF:
public void criaPdf(){
Bitmap bitmap = Utils.screenShot(mContratoWebview);
Document doc = new Document();
File dir = new File(getFilesDir(), "app_imageDir");
if(!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir, "contratoPdf.pdf");
try {
FileOutputStream fOut = new FileOutputStream(file);
PdfWriter.getInstance(doc, fOut);
//open the document
doc.open();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Image image = Image.getInstance(byteArray);
image.scaleToFit(PageSize.A4.getHeight(), PageSize.A4.getWidth());
doc.newPage();
doc.add(image);
} catch (DocumentException de) {
Log.e("PDFCreator", "DocumentException:" + de);
} catch (IOException e) {
Log.e("PDFCreator", "ioException:" + e);
}
finally {
doc.close();
}
mPdfView.fromFile(file)
.pages(0, 1) // all pages are displayed by default
.enableSwipe(true)
.load();
mPdfView.setVisibility(View.VISIBLE);
}
This is what I got so far:
So my problem is: The content of the Webview is too big to fit in the PDF. How can I solve this?