简体   繁体   中英

Android How to create pdf from Crosswalk webview?

Hello I am trying to create PDF from webview in Crosswalk but I didn't find any supporting methods how to do it. Currently I am using this option to create bitmap

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

and then I try to convert the bitmap using itext lib How I can convert a bitmap into PDF format in android as output I get blank pdf (black page).

Here is the code for bitmap:

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();
      }
}

I need just hint :). Thanks a lot. This is my first question I apologize for typos

XWalkView use a standalone SurfaceView(or TextureView) as its output target.The draw or onDraw of XWalkView draws nothing. So you can not rely on it to draw something into a pdf file.

As a workaround, there is another api XWalkView.captureBitmapAsync which can be used to capture the visible webpage into a bitmap. Here is a simple guide about how to use it: 1), Implement 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), Start the capture in UI thread:

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

More details is here: https://crosswalk-project.org/jira/browse/XWALK-5110

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM