从Android生成PDF [英] Generate PDF from Android
问题描述
我想从Android的活动"中创建一个pdf文件.这是代码:
I want to build a pdf file from my Activity in Android. So this is the code:
public File creaOrdinePDF(Ordine ordine,String linkImage,Context context){
doc = new Document();
File dir = null;
String path="";
try {
path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + directoryFolder;
dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
//Toast.makeText(getApplicationContext(), "Directory creata", 10).show();
}
Log.d("PDFCreator", "PDF Path: " + path);
nomeFile="Ordine_negozio_"+ordine.getId();
File file = new File(dir, nomeFile);
FileOutputStream fOut = new FileOutputStream(file);
PdfWriter.getInstance(doc, fOut);
doc.setMargins(5, 5, 30, 30);
doc.open();
Font headerFont = new Font(Font.COURIER, 7.00f, Font.BOLD);
Font tableFont = new Font(Font.COURIER, 8.00f, Font.NORMAL);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
//Bitmap bitmap = BitmapFactory.decodeResource(context
// .getResources(), R.drawable.logo);
Log.d("PDFCreator", "Link immagine " + linkImage);
Bitmap bitmap = BitmapFactory.decodeFile(linkImage);
if(bitmap != null){
Log.d("PDFCreator", "Immagine diversa da null ");
}else{
Log.d("PDFCreator", "Immagine null ");
}
Log.e("PDFCreator","Pippo");
bitmap.compress(Bitmap.CompressFormat.JPEG, 30, stream);
Image myImg = Image.getInstance(stream.toByteArray());
float width = myImg.getWidth();
float height = myImg.getHeight();
float rapp = width / height;
if(width > 260F)
{
myImg.scaleAbsoluteWidth(200F);
myImg.scaleAbsoluteHeight(200F / rapp);
}
if(200F / rapp > 100F)
{
myImg.scaleAbsoluteWidth(rapp * 100F);
myImg.scaleAbsoluteHeight(100F);
}
myImg.setAlignment(Image.MIDDLE);
doc.add(myImg);
Log.e("PDFCreator","aggiunta immagine");
//inserisco il numero dell ordine
Paragraph p2 = new Paragraph("Ordine numero: "+ ordine.getId(),tableFont);
Log.e("PDFCreator","ordine numero "+ordine.getId());
p2.setSpacingAfter(10f);
doc.add(p2);
// add paragraph to document
//doc.add(p1);
float[] dim = {0.7f,3,1.5f,5};
PdfPTable table = new PdfPTable(dim);
table.setWidthPercentage(100);
PdfPCell cell = new PdfPCell(new Paragraph("QTA.", headerFont));
cell.setGrayFill(0.8F);
cell.setUseAscender(true);
cell.setVerticalAlignment(5);
cell.setHorizontalAlignment(0);
cell.setColspan(1);
cell.setBorderWidth(0.5F);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("NOME ARTICOLO", headerFont));
cell.setGrayFill(0.8F);
cell.setUseAscender(true);
cell.setVerticalAlignment(5);
cell.setHorizontalAlignment(0);
cell.setColspan(1);
cell.setBorderWidth(0.5F);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("CODICE ARTICOLO", headerFont));
cell.setGrayFill(0.8F);
cell.setUseAscender(true);
cell.setVerticalAlignment(5);
cell.setHorizontalAlignment(0);
cell.setColspan(1);
cell.setBorderWidth(0.5F);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("NOTE", headerFont));
cell.setGrayFill(0.8F);
cell.setUseAscender(true);
cell.setVerticalAlignment(5);
cell.setHorizontalAlignment(0);
cell.setColspan(1);
cell.setBorderWidth(0.5F);
table.addCell(cell);
Log.e("PDFCreator","size lista articoli "+ordine.getListaArticoli().size());
int i=0;
for(Iterator<ArticoliOrdine> it = ordine.getListaArticoli().iterator(); it.hasNext();)
{
ArticoliOrdine art = it.next();
cell = new PdfPCell(new Paragraph(art.getQuantia()+" pz.", tableFont));
cell.setVerticalAlignment(5);
cell.setHorizontalAlignment(0);
cell.setColspan(1);
cell.setFixedHeight(20);
cell.setBorderWidth(0.5F);
table.addCell(cell);
Log.e("PDFCreator","idArticolo "+ i++);
cell = new PdfPCell(new Paragraph(art.getNomeArticolo(), tableFont));
cell.setVerticalAlignment(5);
cell.setHorizontalAlignment(0);
cell.setColspan(1);
cell.setBorderWidth(0.5F);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(art.getCodiceBarre(), tableFont));
cell.setVerticalAlignment(5);
cell.setHorizontalAlignment(0);
cell.setColspan(1);
cell.setBorderWidth(0.5F);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("", tableFont));
cell.setVerticalAlignment(5);
cell.setHorizontalAlignment(2);
cell.setColspan(1);
cell.setBorderWidth(0.5F);
table.addCell(cell);
}
doc.add(table);
Log.e("PDFCreator", "Fine PDF");
// set footer
Phrase footerText = new Phrase("This is an example of a footer");
HeaderFooter pdfFooter = new HeaderFooter(footerText, true);
doc.setFooter(pdfFooter);
} catch (DocumentException de) {
Log.e("PDFCreator", "DocumentException:" + de);
} catch (IOException e) {
Log.e("PDFCreator", "ioException:" + e);
} catch (Exception e ){
Log.e("PDFCreator", "Exception:" + e);
} finally {
doc.close();
File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + directoryFolder+"/"+nomeFile);
Log.e("PDFCreator", "return file");
return file;
}
}
但是如果我尝试生成它,则会出现此错误:
but if I try to genereta it I have this error:
11-02 12:51:19.276 9589-10325/com.mcsolution.easymanagementandroid.easymanagementandroid E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #5
Process: com.mcsolution.easymanagementandroid.easymanagementandroid, PID: 9589
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Ljava/awt/Color;
at com.lowagie.text.pdf.PdfChunk.color(Unknown Source)
at com.lowagie.text.pdf.PdfDocument.writeLineToContent(Unknown Source)
at com.lowagie.text.pdf.PdfDocument.flushLines(Unknown Source)
at com.lowagie.text.pdf.PdfDocument.newPage(Unknown Source)
at com.lowagie.text.pdf.PdfDocument.close(Unknown Source)
at com.lowagie.text.Document.close(Unknown Source)
at com.mcsolution.easymanagementandroid.print.Print.creaOrdinePDF(Print.java:186)
at com.mcsolution.easymanagementandroid.ordine.creaOrdine$RunnableGeneraOrdine_PDF.doInBackground(creaOrdine.java:778)
at com.mcsolution.easymanagementandroid.ordine.creaOrdine$RunnableGeneraOrdine_PDF.doInBackground(creaOrdine.java:772)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.ClassNotFoundException: Didn't find class "java.awt.Color" on path: DexPathList[[dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-support-annotations-23.3.0_469f8f3ca3f9e0c059b9c6f2d04acc51933988a6-classes.dex",
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-slice_9-classes.dex",
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-slice_8-classes.dex",
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-slice_7-classes.dex",
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-slice_6-classes.dex",
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-slice_5-classes.dex",
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-slice_4-classes.dex",
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-slice_3-classes.dex",
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-slice_2-classes.dex",
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-slice_1-classes.dex",
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-slice_0-classes.dex",
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-lowagie-2.1.7_6c71a44cd1fb2c264f7135848cfb4fd4358dd002-classes.dex",
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-internal_impl-23.3.0_459b0ff04633a2c5ffc128ab96df15303edb9d39-classes.dex",
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-gson-2.2.4_e859ceafcaa49a8708d05a8e64ce9bdc0fd2e6a0-classes.dex",
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-com.android.support-support-vector-drawable-23.3.0_32569b030fbf7638ea3bff8f6335741d192c74ac-classes.dex",
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/sl
推荐答案
com.lowagie
iText(2.1.7或更早版本)在Android上不起作用.您需要 iText v5 (com.itextpdf
),更具体地说是Android端口 iTextG ,其中不包括Android平台上不可用的AWT类: http://developers.itextpdf.com/itextg-android
com.lowagie
iText (2.1.7 or older) won't work on Android. You need iText v5 (com.itextpdf
), more specifically the Android port iTextG, which excludes the AWT classes that are not available on the Android platform: http://developers.itextpdf.com/itextg-android
这就是为什么你得到
Caused by: java.lang.ClassNotFoundException: Didn't find class "java.awt.Color"
为您提供信息,iText(G)v5为AGPLv3,这意味着您的Android应用也必须为AGPLv3(即,向最终用户发布源代码等),否则您需要购买商业许可证./em>
For your information, iText(G) v5 is AGPLv3, which means that your Android app must also be AGPLv3 (i.e. release source code to your end users etc), or you need to purchase a commercial license.
这篇关于从Android生成PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!