【问题标题】:Print Textviews and webview from fragment从片段打印 Textviews 和 webview
【发布时间】:2022-08-09 03:52:58
【问题描述】:

我正在使用这样的代码从 webview 创建 pdf

PrintManager printManager = (PrintManager) this
                .getSystemService(Context.PRINT_SERVICE); 
PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter(\"PDF\");
printManager.print(\"PDF\", printAdapter,
                new PrintAttributes.Builder().build());

我应该怎么做,添加到这个 pdf 创建一些 TextView 因为我需要向我的片段添加额外的字段?

了解itextg,但我需要一些免费的东西。

    标签: android pdf printing webview textview


    【解决方案1】:

    选择一个选项以在 webview 中将字段实现为 html

    【讨论】:

      【解决方案2】:

      您可以从 webview 中获取位图,然后使用位图画布在位图上绘制 textview。

      从那里,您可以将位图转换为 pdf 并共享 pdf。

      以下是将 webview 转换为位图的代码,并将位图转换为 pdf 然后共享它,要求用户选择其打印机应用程序:

      public static void sharePdfFile(WebView webView, Context context)
      {
          Bitmap bitmap = webviewToBitmap( webView );
          PrintedPdfDocument pdf =  bitmapToPdf( bitmap, context );
          File file = pdfToFile( pdf, context );
          shareFile( file,"application/pdf", context );
      }
      
      private static void shareFile(File file, String contentType, Context context)
      {
          Uri uri = FileProvider.getUriForFile(
              context,
              context.getPackageName() + ".fileprovider",
              file);
          Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
          shareIntent.setType(contentType);
          shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
          shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
          Toast.makeText(
              context,
              "Choose your printer app",
              Toast.LENGTH_LONG
          ).show();
          context.startActivity( shareIntent );
      }
      
      private static File pdfToFile(PrintedPdfDocument printedPdfDocument, Context context)
      {
          File file = new File(context.getFilesDir(), "share.pdf");
          try {
              FileOutputStream outputStream = new FileOutputStream(file);
              printedPdfDocument.writeTo(outputStream);
              outputStream.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
          printedPdfDocument.close();
          return file;
      }
      
      
      private static PrintedPdfDocument bitmapToPdf(Bitmap bitmap, Context context)
      {
          PrintAttributes printAttributes = new PrintAttributes.Builder()
              .setColorMode(PrintAttributes.COLOR_MODE_COLOR)
              .setMediaSize(PrintAttributes.MediaSize.ISO_A4)
              .setMinMargins(PrintAttributes.Margins.NO_MARGINS)
              .setResolution(new PrintAttributes.Resolution("1", "label", 300, 300))
              .build();
          PrintedPdfDocument printedPdfDocument = new PrintedPdfDocument(context, printAttributes);
          PdfDocument.Page pdfDocumentPage = printedPdfDocument.startPage(1);
          Canvas pdfCanvas = pdfDocumentPage.getCanvas();
          bitmap = scaleBitmapToHeight(bitmap, pdfCanvas.getHeight());
          pdfCanvas.drawBitmap(bitmap, 0f, 0f, null);
          printedPdfDocument.finishPage(pdfDocumentPage);
          return printedPdfDocument;
      }
      
      private static Bitmap webviewToBitmap(WebView webView) {
          webView.measure(
              View.MeasureSpec.makeMeasureSpec(
                  0,
                  View.MeasureSpec.UNSPECIFIED
              ),
              View.MeasureSpec.makeMeasureSpec(
                  0,
                  View.MeasureSpec.UNSPECIFIED
              )
          );
          int webViewWidth = webView.getMeasuredWidth();
          int webViewHeight = webView.getMeasuredHeight();
          webView.layout(0,0, webViewWidth, webViewHeight );
          Bitmap bitmap = Bitmap.createBitmap(webViewWidth, webViewHeight, Bitmap.Config.ARGB_8888);
          Canvas canvas = new Canvas(bitmap);
          canvas.drawBitmap(bitmap, 0, bitmap.getHeight(), new Paint());
          webView.draw(canvas);
          return bitmap;
      }
      
      private static Bitmap scaleBitmapToHeight(Bitmap bitmap, int maxHeight) {
          int height = bitmap.getHeight();
          if(height > maxHeight) {
              int width = bitmap.getWidth();
              float scalePercentage = ((float)maxHeight) / height;
              return Bitmap.createScaledBitmap(bitmap, (int) (width * scalePercentage), maxHeight, false);
          }
          return bitmap;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多