[英]How to create PDF from webview in android?
I am displaying webpage
in webview
. 我正在
webview
显示webpage
。 Now how to create PDF from webview ? 现在如何从webview创建PDF ?
For Example : webview loads URL is "www.google.co.in"
. 例如:webview加载URL为
"www.google.co.in"
。 Now how to save this webpage as image and create pdf ?? 现在如何将此网页保存为图像并创建pdf?
any help would be appreciated 任何帮助,将不胜感激
Try like this 试试这样吧
WebView have inbuilt method called setPictureListener
Use that method as below WebView具有名为
setPictureListener
内置方法使用该方法如下所示
webView1.setPictureListener(new PictureListener() {
public void onNewPicture(WebView view, Picture picture) {
if (picture != null) {
try {
bmp = pictureDrawable2Bitmap(new PictureDrawable(
picture));
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
For obtaining bitmap i have used pictureDrawable2Bitmap
and here is that one 为了获得位图,我使用了
pictureDrawable2Bitmap
,这是一个
private static Bitmap pictureDrawable2Bitmap(PictureDrawable pictureDrawable) {
Bitmap bitmap = Bitmap.createBitmap(
pictureDrawable.getIntrinsicWidth(),
pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
return bitmap;
}
Now Your Bitmap is ready,Now set webview client as below 现在您的位图已准备就绪,现在将webview客户端设置如下
webView1.setWebViewClient(new myWebClient());
And here is myWebClient
这是
myWebClient
public class myWebClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
Log.i("OnPageLoadFinished", url);
img.setImageBitmap(bmp);
}
As shown on page load finished i have set image bitmap which is snap of current loaded url on your webview 如页面加载完成所示,我已经设置了图像位图,它是webview上当前加载的URL的快照
Now Bitmap is ready pass that bitmap to Pdf using IText Library 现在,Bitmap已准备好使用IText Library将该位图传递给Pdf
Here is an example of writing pdf with image on iText Use Below function for that 下面是在iText上使用下面的函数编写带有图像的pdf的示例
public void SimplePDFTable() throws Exception {
File direct = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/AamirPDF");
if (!direct.exists()) {
if (direct.mkdir()) {
Toast.makeText(MainActivity.this,
"Folder Is created in sd card", Toast.LENGTH_SHORT)
.show();
}
}
String test = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/AamirPDF";
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(test
+ "/mypdf.pdf"));
document.open();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Image image = Image.getInstance(byteArray);
image.scaleToFit(PageSize.A4.getHeight(), PageSize.A4.getWidth());
document.add(image);
document.close();
}
Good Luck 祝好运
As of API 19 (KitKat), Android now lets you print a webview directly. 从API 19(KitKat)开始,Android现在允许您直接打印webview 。 Moreover, the same PrintManager API can be used to generate a PDF from a WebView without any external libraries.
此外,相同的PrintManager API可用于从WebView生成PDF而无需任何外部库。
As the example code shows, just get the PrintManager , create a PrintDocumentAdapter from the WebView in question, then create a new PrintJob and your user should see the option to save it to a file as PDF or print to a cloud printer. 如示例代码所示,只需获取PrintManager ,从相关的WebView创建PrintDocumentAdapter ,然后创建一个新的PrintJob ,您的用户应该看到将其保存为PDF文件或打印到云打印机的选项。 On newer Androids than 4.4 they'll also get a visual preview of what will be printed/PDF'd.
在比4.4更新的Androids上,他们还可以直观地预览将要打印/ PDF的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.