我目前正在尝试为我的Android应用程序添加PDF导出功能。
这包括填充Url,将其放入WebView,然后使用适用的代码将该WebView的内容写入PDF:
public Bitmap viewToBitmap(View view){ Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),view.getHeight(),Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(帆布); 返回位图; }
public void deleteRecursive(File fileOrDirectory) {
if (fileOrDirectory.isDirectory())
for (File child : fileOrDirectory.listFiles())
deleteRecursive(child);
fileOrDirectory.delete();
}
public void bmpToPdfMail(Bitmap bitmap) {
String GVCode = String.valueOf(System.currentTimeMillis() + "_InvoiceTest");
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/Invoices");
if (myDir.exists()) {
deleteRecursive(myDir);
}
myDir.mkdirs();
String fname = GVCode + ".jpg";
File file = new File(myDir, fname);
if (file.exists()) {
deleteRecursive(file);
}
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
Document document = new Document();
String input = Environment.getExternalStorageDirectory().toString() + "/Invoices/" + GVCode + ".jpg";
String output = Environment.getExternalStorageDirectory().toString() + "/Invoices/" + GVCode + ".pdf";
String stringPrintDirectory = Environment.getExternalStorageDirectory().toString() + "/Invoices/";
File myDirPdf = new File(stringPrintDirectory);
if (!myDirPdf.exists()) {
myDirPdf.mkdirs();
}
try {
FileOutputStream fos = new FileOutputStream(output);
PdfWriter writer = PdfWriter.getInstance(document, fos);
writer.open();
document.open();
Image img = Image.getInstance(input);
document.setPageSize(img);
document.newPage();
img.setAbsolutePosition(0, 0);
document.add(img);
document.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
如何从WebView创建PDF,这是隐藏的部分?
然而,当生成PDF时,我只获得了可见区域 WebView,因此屏幕外的信息被切断了。
如何在WebView中获取整个内容(可见或不可见部分)以从我的Android应用程序导出PDF文件??
答案 0 :(得分:0)
在页面成功加载后,尝试使用此方法获取位图。
public Bitmap viewToBitmap(WebView v) {
Picture picture = v.capturePicture();
Bitmap b = Bitmap.createBitmap( picture.getWidth(),
picture.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas( b );
picture.draw( c );
return b;
}
因为,在api级别19中弃用capturePicture()
,使用
public Bitmap viewToBitmap(WebView v) {
float scale = v.getScale(); // Used to capture whole the content
int webViewHeight = (int)(v.getContentHeight() * scale);
Bitmap image = Bitmap.createBitmap(v.getWidth(), webViewHeight,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(image);
v.draw(canvas);
return image;
}
使用位图生成PDF。在此之前,尝试查看位图以确定pdf生成代码是否存在问题或者是什么!!
答案 1 :(得分:0)
我找到了一个解决方案,不是那么优雅而是在工作:
wb.measure(View.MeasureSpec.makeMeasureSpec(
View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
wb.layout(0, 0, wb.getMeasuredWidth(), wb.getMeasuredHeight());
PdfDocument document = new PdfDocument();
int pageNumber = 1;
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(wb.getMeasuredWidth(),
wb.getMeasuredHeight(), pageNumber).create();
PdfDocument.Page page = document.startPage(pageInfo);
//wb.draw(page.getCanvas());
wb.draw(page.getCanvas());
document.finishPage(page);
这会根据需要创建一个适合整个WebView的PDF页面。 希望这有帮助!