package com.example.demo.controller;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import sun.misc.BASE64Encoder;
import java.io.*;
/**
- 创建一个空的PDF
*/
// 需要下 apache pdfbox包和apache.commons.loggin乌,下载地址:http://pdfbox.apache.org/download.cgi 和 http://commons.apache.org/proper/commons-logging/download_logging.cgi
// 在本示例中下载使用的是:pdfbox-2.0.7.jar
// 将下载的pdfbox-2.0.7.jar添加到Eclipse项目依懒库中。
// 右键点击:“java_apache_pdf_box”->“Bulid Path”->“Add External Artchives…”,然后选笃下载的"pdfbox-2.0.7.jar"和"commons-logging-1.2.jar"文件
public class CreatNullPDF {
public static void main(String args[]) throws IOException {
String s= "YWdlcyAyIDAgUgo+PgplbmRvYmoKMiAwIG9iago8PAovVHlwZSAvUGFnZXMKL0tpZHMgWzMgMCBS\n" +
"XQovQ291bnQgMQo+PgplbmRvYmoKMyAwIG9iago8PAovVHlwZSAvUGFnZQovTWVkaWFCb3ggWzAu\n" +
"MCAwLjAgNjEyLjAgNzkyLjBdCi9QYXJlbnQgMiAwIFIKPj4KZW5kb2JqCnhyZWYKMCA0CjAwMDAw\n" +
"MDAwMDAgNjU1MzUgZg0KMDAwMDAwMDAxNSAwMDAwMCBuDQowMDAwMDAwMDc4IDAwMDAwIG4NCjAw\n" +
"MDAwMDAxMzUgMDAwMDAgbg0KdHJhaWxlcgo8PAovUm9vdCAxIDAgUgovSUQgWzxBMjE4N0EyODlC\n" +
"QUE5RDc0MDU5REY3OUQ2QTMxQTA5MT4gPEEyMTg3QTI4OUJBQTlENzQwNTlERjc5RDZBMzFBMDkx\n" +
"Pl0KL1NpemUgNAo+PgpzdGFydHhyZWYKMjE0CiUlRU9GCg==";
System.out.println(s.length());
// creatPDF("D:","abc");
// String s = PDFToBase64(new File(“D:\abc.pdf”));
// System.out.println(s);
}
public static boolean creatPDF(String url,String name){
boolean is=false;
try {
// Creating PDF document object
PDDocument document = new PDDocument();
// Add an empty page to it
document.addPage(new PDPage());
// Saving the document
document.save(url+"/"+name+".pdf");
System.out.println(name+"文件创建成功!");
// Closing the document
document.close();
is=true;
}catch (Exception e){
is=false;
System.out.println(name+"文件创建出错!");
}
return is;
}
/**
* Description: 将pdf文件转换为Base64编码
* @param 要转的的pdf文件
* @Author fuyuwei
* Create Date: 2015年8月3日 下午9:52:30
*/
public static String PDFToBase64(File file) {
BASE64Encoder encoder = new BASE64Encoder();
FileInputStream fin =null;
BufferedInputStream bin =null;
ByteArrayOutputStream baos = null;
BufferedOutputStream bout =null;
try {
fin = new FileInputStream(file);
bin = new BufferedInputStream(fin);
baos = new ByteArrayOutputStream();
bout = new BufferedOutputStream(baos);
byte[] buffer = new byte[1024];
int len = bin.read(buffer);
while(len != -1){
bout.write(buffer, 0, len);
len = bin.read(buffer);
}
//刷新此输出流并强制写出所有缓冲的输出字节
bout.flush();
byte[] bytes = baos.toByteArray();
return encoder.encodeBuffer(bytes).trim();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fin.close();
bin.close();
bout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}https://www.yiibai.com/javaexamples/create_empty_pdf_document.html