天天看点

android pdf to html,android 开发 将view保存为image的实现及将html保存为pdf格式

一、将view保存为image

public class ImageHelper {

///将view保存到bitmap中

public Bitmap createBitmap(View view) {

int widthSpec = View.MeasureSpec.makeMeasureSpec(view.getLayoutParams().width, View.MeasureSpec.EXACTLY);

int heightSpec = View.MeasureSpec.makeMeasureSpec(view.getLayoutParams().height, View.MeasureSpec.EXACTLY);

view.measure(widthSpec, heightSpec);

int measureWidth = view.getMeasuredWidth();

int measureHeight = view.getMeasuredHeight();

view.layout(0, 0, measureWidth, measureHeight);

int width = view.getWidth();

int height = view.getHeight();

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(bitmap);

view.draw(canvas);

return bitmap;

}

//public String saveBitmap(Context context,Bitmap mBitmap,String savePath,String fileName) {

public String saveBitmap(Context context,Bitmap mBitmap,String savePath) {

File filePic;

try {

filePic = new File(savePath);

if (!filePic.exists()) {

filePic.getParentFile().mkdirs();

filePic.createNewFile();

}

FileOutputStream fos = new FileOutputStream(filePic);

mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

fos.flush();

fos.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

return "保存图片到本地时报错:"+e.getMessage();

}

// 其次把文件插入到系统图库

// try {

// MediaStore.Images.Media.insertImage(context.getContentResolver(),

// filePic.getAbsolutePath(), fileName, null);

// //MyToastUtils.showShortToast(context, "保存成功");

// } catch (FileNotFoundException e) {

// //MyToastUtils.showShortToast(context, "保存失败");

// e.printStackTrace();

// return "把文件插入到系统图库失败:"+e.getMessage();

// }

// 最后通知图库更新

context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,

Uri.fromFile(new File(filePic.getPath()))));

return "Success";

}

}

二、将html代码保存为pdf

需要引入三个jar包:itextpdf-5.5.jar、jsoup-1.7.jar、xmlworker-5.5.3.jar

public class PDFHelper {

///保存文件的方法

public String createPDF(String rawHTML, String fileName, ContextWrapper context){

File file = new File(fileName);

try{

Document document = new Document();

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));

document.open();

// HTML

String htmlText = Jsoup.clean( rawHTML, Whitelist.relaxed() );

InputStream inputStream = new ByteArrayInputStream( htmlText.getBytes() );

// PDF

XMLWorkerHelper.getInstance().parseXHtml(writer, document,

inputStream, null, Charset.defaultCharset(), new MyFont());

document.close();

return "";

} catch (FileNotFoundException e) {

e.printStackTrace();

return "Error|"+e.getMessage();

} catch (DocumentException e) {

e.printStackTrace();

return "Error|"+e.getMessage();

} catch (IOException e) {

e.printStackTrace();

return "Error|"+e.getMessage();

}

}

public class MyFont implements FontProvider {

private static final String FONT_PATH = "/system/fonts/DroidSansFallback.ttf";

private static final String FONT_ALIAS = "my_font";

public MyFont() {

FontFactory.register(FONT_PATH, FONT_ALIAS);

}

@Override

public Font getFont(String fontname, String encoding, boolean embedded,

float size, int style, BaseColor color) {

return FontFactory.getFont(FONT_ALIAS, BaseFont.IDENTITY_H,

BaseFont.EMBEDDED, size, style, color);

}

@Override

public boolean isRegistered(String name) {

return name.equals(FONT_ALIAS);

}

}

}