![](/img/trans.png)
[英]PDF size too large generating through Android PDFDocument. And while using pdfbox it is cutting image in output
[英]Generating a PDF with custom size in android with PdfDocument
PdfDocument是一個可以從 Android 視圖生成 PDF 的類。 您只需將視圖添加到PdfDocument
,然后將 PDF 保存到內存中。
但是,我不想添加已在屏幕中呈現的視圖,因為此類視圖僅適用於智能手機屏幕,並且我想為打印機生成 PDF 文檔。
因此,我想在不渲染的情況下傳遞一個視圖。 雖然我當然可以這樣做,但尺寸和比例將如何決定? 如何在我的 PDF 中控制它?
更新:
按照下面來自clotodex的答案,我做了:
LayoutInflater inflater = getLayoutInflater();
View linearview;
linearview = inflater.inflate(R.layout.printed_order,
findViewById(android.R.id.content),
false);
PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(2480, 3508, 0).create();
PdfDocument.Page page = document.startPage(pageInfo);
linearview.draw(page.getCanvas());
document.finishPage(page);
OutputStream outStream;
File file = new File(getExternalFilesDir(null), "pedido.PDF");
try {
outStream = new FileOutputStream(file);
document.writeTo(outStream);
document.close();
outStream.flush();
outStream.close();
但我得到一個空的 PDF。 如果我做
View view = findViewById(android.R.id.content);
view.draw(page.getCanvas());
代替
linearview.draw(page.getCanvas());
我得到了我正在進行的活動,這證實了一切都是正確的,除了我試圖打印的視圖。
看看這個問題render view off-screen in android 。
在 Android 中,視圖的度量由父布局給出。 這意味着您可以使用所需的測量值創建新布局,並使用attachToRoot=false
和root=your_new_layout
您的視圖。
渲染視圖后,您現在可以繼續將渲染視圖繪制為 pdf(按照鏈接中的說明進行操作,或在繪制到頁面畫布之前將其轉換為位圖)。
我想如果你只想打印到pdf,你可以使用PrintManager
相關的API。 有關詳細信息,您可以查看此API 。
這是pdf的一些示例代碼。
// method for PrintAdapter
override fun onLayout(
oldAttributes: PrintAttributes?,
newAttributes: PrintAttributes,
cancellationSignal: CancellationSignal?,
callback: LayoutResultCallback,
extras: Bundle?
) {
// Create a new PdfDocument with the requested page attributes
pdfDocument = PrintedPdfDocument(activity, newAttributes)
// Compute the expected number of printed pages
val pages = computePageCount(newAttributes)
if (pages > 0) {
// Return print information to print framework
PrintDocumentInfo.Builder("print_output.pdf")
.setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
.setPageCount(pages)
.build()
.also { info ->
// Content layout reflow is complete
callback.onLayoutFinished(info, true)
}
} else {
// Otherwise report an error to the print framework
callback.onLayoutFailed("Page count calculation failed.")
}
}
我使用了PrintHelper
你可以在這里找到它的文檔
public void print() {
if(PrintHelper.systemSupportsPrint()){
final Bitmap bm;
try {
bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bm.eraseColor(Color.WHITE);// otherwise it will be trans
final Canvas canvas = new Canvas(bm);
drawOn(canvas)// draw on canvas
}catch (OutOfMemoryError er) {
// out of memory error
return;
}
if(bm == null) return;
final PrintHelper printHelper = new PrintHelper(getContext());
printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT);
printHelper.printBitmap("fileName", bm, new PrintHelper.OnPrintFinishCallback() {
@Override
public void onFinish() {
bm.recycle();
}
});
}else{
// print not supported
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.