如何在Android项目中将webview转换为PDF

发布时间:2021-02-20 17:08:02

来源:亿速云

阅读:105

作者:Leah

本篇文章给大家分享的是有关如何在Android项目中将webview转换为PDF,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

完整代码如下:File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM + "/PDFTest.pdf");

File dexCacheFile;

// 获取需要打印的webview适配器

PrintDocumentAdapter printAdapter;

PageRange[] ranges;

ParcelFileDescriptor descriptor;

/**

a* @param webView

*/

private void printPDFFile(WebView webView) {

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {

/**

* android 5.0之后,出于对动态注入字节码安全性德考虑,已经不允许随意指定字节码的保存路径了,需要放在应用自己的包名文件夹下。

*/

//新的创建DexMaker缓存目录的方式,直接通过context获取路径

dexCacheFile = getDir("dex", 0);

if (!dexCacheFile.exists()) {

dexCacheFile.mkdir();

}

try {

//创建待写入的PDF文件,pdfFilePath为自行指定的PDF文件路径

if (file.exists()) {

file.delete();

}

file.createNewFile();

descriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);

// 设置打印参数

PrintAttributes attributes = new PrintAttributes.Builder()

.setMediaSize(PrintAttributes.MediaSize.ISO_A4)

.setResolution(new PrintAttributes.Resolution("id", Context.PRINT_SERVICE, 300, 300))

.setColorMode(PrintAttributes.COLOR_MODE_COLOR)

.setMinMargins(PrintAttributes.Margins.NO_MARGINS)

.build();

//打印所有界面

ranges = new PageRange[]{PageRange.ALL_PAGES};

printAdapter = webView.createPrintDocumentAdapter();

// 开始打印

printAdapter.onStart();

printAdapter.onLayout(attributes, attributes, new CancellationSignal(), getLayoutResultCallback(new InvocationHandler() {

@Override

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

if (method.getName().equals("onLayoutFinished")) {

// 监听到内部调用了onLayoutFinished()方法,即打印成功

onLayoutSuccess();

} else {

// 监听到打印失败或者取消了打印

}

return null;

}

}, dexCacheFile.getAbsoluteFile()), new Bundle());

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* @throws IOException

*/

private void onLayoutSuccess() throws IOException {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

PrintDocumentAdapter.WriteResultCallback callback = getWriteResultCallback(new InvocationHandler() {

@Override

public Object invoke(Object o, Method method, Object[] objects) throws Throwable {

if (method.getName().equals("onWriteFinished")) {

Toast.makeText(MainActivity.this,"Success",Toast.LENGTH_SHORT).show();

// PDF文件写入本地完成,导出成功

Log.e("onLayoutSuccess","onLayoutSuccess");

} else {

Toast.makeText(MainActivity.this,"导出失败",Toast.LENGTH_SHORT).show();

}

return null;

}

}, dexCacheFile.getAbsoluteFile());

//写入文件到本地

printAdapter.onWrite(ranges, descriptor, new CancellationSignal(), callback);

}else {

Toast.makeText(MainActivity.this,"不支持4.4.以下",Toast.LENGTH_SHORT).show();

}

}

@SuppressLint("NewApi")

public static PrintDocumentAdapter.LayoutResultCallback getLayoutResultCallback(InvocationHandler invocationHandler, File dexCacheDir) throws IOException {

return ProxyBuilder.forClass(PrintDocumentAdapter.LayoutResultCallback.class)

.dexCache(dexCacheDir)

.handler(invocationHandler)

.build();

}

@SuppressLint("NewApi")

public static PrintDocumentAdapter.WriteResultCallback getWriteResultCallback(InvocationHandler invocationHandler, File dexCacheDir) throws IOException {

return ProxyBuilder.forClass(PrintDocumentAdapter.WriteResultCallback.class)

.dexCache(dexCacheDir)

.handler(invocationHandler)

.build();

}

以上就是如何在Android项目中将webview转换为PDF,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐