Android如何从Crosswalk webview创建pdf?

时间:2016-05-23 12:40:02

标签: android pdf webview crosswalk

您好我正在尝试从Crosswalk中的webview创建PDF,但我没有找到任何支持方法如何做到这一点。目前我正在使用此选项来创建位图

How can I capture the whole view into a Bitmap when using crosswalk to display webpage?

然后我尝试使用itext lib转换位图 How I can convert a bitmap into PDF format in android作为输出我得到空白pdf(黑页)。

以下是位图的代码:

private void createBitmap (XWalkView view) {
    String pathToFile = Environment.getExternalStorageDirectory() +"/bitmap.jpg";
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache(true);
    Bitmap btmp = view.getDrawingCache();
    Canvas canvas = new Canvas(btmp);
    Paint paint = new Paint ();
    int height = btmp.getHeight();
    canvas.drawBitmap(btmp, 0, height, paint);
    view.draw(canvas);

    try {
      OutputStream fOut = null;
      File file = new File(pathToFile);
      fOut = new FileOutputStream(file);
      btmp.compress(Bitmap.CompressFormat.JPEG, 50, fOut);
      fOut.flush();
      fOut.close();
      btmp.recycle();
      view.setDrawingCacheEnabled(false);
      view.destroyDrawingCache();
      } catch (Exception e) {
          e.printStackTrace();
      }
}

我需要提示:)。非常感谢。这是我为拼写错误道歉的第一个问题

1 个答案:

答案 0 :(得分:0)

XWalkView使用独立的SurfaceView(或TextureView)作为其输出目标.XWalkView的draw或onDraw不会显示任何内容。所以你不能依赖它来绘制一些pdf文件。

作为一种解决方法,还有另一个api XWalkView.captureBitmapAsync,可用于将可见网页捕获到位图中。这是一个关于如何使用它的简单指南: 1),实现XWalkGetBitmapCallback

    import org.xwalk.core.XWalkGetBitmapCallback;
    class XWalkGetBitmapCallbackImpl extends XWalkGetBitmapCallback {
    public XWalkGetBitmapCallbackImpl() {
        super();
    }
    //Note: onFinishGetBitmap happens at the same thread as captureBitmapAsync, usually the UI thread.
    @Override
    public void onFinishGetBitmap(Bitmap bitmap, int response) {
        //if response == 0, save this bitmap into a jpg file //otherwise errors.
    }
}

2),在UI线程中开始捕获:

private void captureContent() {
    if ( xWalkView == null) return;
    mXWalkGetBitmapCallback = new XWalkGetBitmapCallbackImpl();
    xWalkView.captureBitmapAsync(mXWalkGetBitmapCallback);
}

更多详情请点击此处:https://crosswalk-project.org/jira/browse/XWALK-5110