gpt4 book ai didi

android - 在 Android 中使用来自网络 URL 的图像从 View 生成 PDF 文件

转载 作者:太空狗 更新时间:2023-10-29 13:48:27 25 4
gpt4 key购买 nike

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

文档应该包含一个标题,它是一个静态 ImageView 、一些 TextView 和一个带有一堆图像的 RecyclerView,

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

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

这是创建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;
}
}

这是我绑定(bind) 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 库来解决这个问题,

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

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

关于android - 在 Android 中使用来自网络 URL 的图像从 View 生成 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50552026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com