使用 Android 中来自网络 URL 的图像从视图生成 PDF 文件

Generate PDF files from view with Images from network URL in Android

我正在使用本机 PDF 库生成 PDF 文件 (android.graphics.pdf.PdfDocument)

文档应该包含一个 header 静态 imageView、一些文本视图和一个带有一堆图像的 RecyclerView,

除了 recyclerview 之外的所有内容都正确显示,

recyclerview 正在使用 Glide 库加载图像,但它们既未加载也未显示,我尝试 pre-fetching 使用该库,但它也没有任何作用,

这是创建 pdf 的代码

    public File createPDF(PostDB postDB) {
    int addition = 0;
    String postDBContent = postDB.getContent();
    if (postDBContent != null) {
        int estimatedLineCount = postDBContent.length() / 43;
        String[] lines = postDBContent.split("\r\n|\r|\n");
        estimatedLineCount += lines.length;
        addition = (estimatedLineCount - 43) * 60;
    }


    List<String> imagesUrls = postDB.getImagesUrls();
    for (String url : imagesUrls) {
        Glide.with(context).load(url).preload();
    }


    try {
        FileOutputStream fOut = context.openFileOutput(LOCAL_PATH, Context.MODE_PRIVATE);
        PdfDocument document = new PdfDocument();
        PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(1800, 3900 + addition + 1000, 1).create();
        PdfDocument.Page page = document.startPage(pageInfo);
        Canvas canvas = page.getCanvas();
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View content;
        if (inflater != null) {
            final int[] count = {0};
            content = inflater.inflate(R.layout.pdf_layout, null);
            ButterKnife.bind(this, content);
            publisherTextView.setText(postDB.getAuthor());
            Date date = new Date(postDB.getDate());
            DateFormat dateFormat = DateFormat.getInstance();
            String format = dateFormat.format(date);
            dateTextView.setText(format);
            titleTextView.setText(postDB.getTitle());
            contentPreviewTextView.setText(postDBContent);
            recyclerViewImages.setLayoutManager(new GridLayoutManager(context, 2));
            recyclerViewImages.setAdapter(new PDFImageAdapter(postDB.getImagesUrls()));
            String text = postDB.getCity() + ", " + postDB.getCountry();
            location.setText(text);
            int measureWidth = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getWidth(), View.MeasureSpec.EXACTLY);
            int measuredHeight = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getHeight(), View.MeasureSpec.EXACTLY);
            content.measure(measureWidth, measuredHeight);
            content.layout(0, 0, page.getCanvas().getWidth(), page.getCanvas().getHeight());
            content.draw(canvas);

        }
        document.finishPage(page);
        document.writeTo(fOut);
        document.close();
        fOut.close();
        return new File(context.getFilesDir().getPath() + "/" + LOCAL_PATH);
    } catch (IOException e) {
        Log.i("error", e.getLocalizedMessage());
        return null;
    }
}

这是我绑定 ViewHolders 的代码

 public void setData(String url) {
    Glide.with(imageView.getContext()).load(url)
            .listener(new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                    Timber.d("onLoadFailed");
                    return true;
                }

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    Timber.d("onResourceReady");
                    return true;
                }
            })
            .into(imageView);
}

调用数据onBind方法,并调用setData方法。但是从来没有调用过 Glide 的回调。

我使用 iText 库来解决这个问题,

例如,这是我用来添加图像的代码。这样你就不需要任何视图了。

Rectangle pageSize = PageSize.A4;
Document document = new Document();
PdfWriter.getInstance(document, context.openFileOutput(LOCAL_PATH, Context.MODE_PRIVATE));
document.open();
 document.setPageSize(pageSize);
   document.addCreationDate();
   document.addAuthor(context.getString(R.string.pdf_creator));
   document.addCreator(context.getString(R.string.pdf_creator));
Image image = Image.getInstance(filename);
   image.scaleToFit(300f, 150f);
   image.setAbsolutePosition(absoluteX, absoluteY);
   document.add(image);
document.close();