简体   繁体   English

Android如何从Crosswalk Webview创建pdf?

[英]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. 您好,我正在尝试从Crosswalk中的webview创建PDF,但没有找到任何支持方法来实现此目的。 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). 然后我尝试使用itext lib转换位图如何在android中将位图转换为PDF格式作为输出我得到空白的pdf(黑页)。

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. XWalkView使用独立的SurfaceView(或TextureView)作为其输出目标.XWalkView的draw或onDraw不会绘制任何内容。 So you can not rely on it to draw something into a pdf file. 因此,您不能依靠它将某些内容绘制到pdf文件中。

As a workaround, there is another api XWalkView.captureBitmapAsync which can be used to capture the visible webpage into a bitmap. 作为一种解决方法,还有另一个api XWalkView.captureBitmapAsync可用于将可见网页捕获为位图。 Here is a simple guide about how to use it: 1), Implement XWalkGetBitmapCallback 这是有关如何使用它的简单指南: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), Start the capture in UI thread: 2),在UI线程中启动捕获:

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 更多详细信息在这里: https : //crosswalk-project.org/jira/browse/XWALK-5110

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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