JAVA 将图片转换成pdf文件

/**
* 将图片转换成pdf文件
*imgFilePath 需要被转换的img所存放的位置。 例如imgFilePath="D:\\projectPath\\55555.jpg";
*pdfFilePath 转换后的pdf所存放的位置 例如pdfFilePath="D:\\projectPath\\test.pdf";
* @param image
* @return
* @throws IOException 
*/


public boolean imgToPdf(String imgFilePath, String pdfFilePath)throws IOException {
File file=new File(imgFilePath);
if(file.exists()){
Document document = new Document();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(pdfFilePath);
PdfWriter.getInstance(document, fos);

// 添加PDF文档的某些信息,比如作者,主题等等
document.addAuthor("arui");
document.addSubject("test pdf.");
// 设置文档的大小
document.setPageSize(PageSize.A4);
// 打开文档
document.open();
// 写入一段文字
//document.add(new Paragraph("JUST TEST ..."));
// 读取一个图片
Image image = Image.getInstance(imgFilePath);
float imageHeight=image.getScaledHeight();
float imageWidth=image.getScaledWidth();
int i=0;
while(imageHeight>500||imageWidth>500){
image.scalePercent(100-i);
i++;
imageHeight=image.getScaledHeight();
imageWidth=image.getScaledWidth();
System.out.println("imageHeight->"+imageHeight);
System.out.println("imageWidth->"+imageWidth);
}

image.setAlignment(Image.ALIGN_CENTER); 
//     //设置图片的绝对位置
// image.setAbsolutePosition(0, 0);
// image.scaleAbsolute(500, 400);
// 插入一个图片
document.add(image);
} catch (DocumentException de) {
System.out.println(de.getMessage());
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
document.close();
fos.flush();
fos.close();
return true;
}else{
return false;
}
}
可以使用开源的Java库iText来将HTML文件转换成PDF。具体实现步骤如下: 1. 首先,需要引入iText库。可以通过Maven或手动下载jar包的方式引入。 2. 使用iText中的PdfWriter类创建一个新的PDF文件。 ```java Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile)); document.open(); ``` 3. 使用iText中的XMLWorkerHelper类将HTML文件转换成PDF。 ```java // 读取HTML文件 String html = FileUtils.readFileToString(new File(htmlFile), "UTF-8"); // 创建字体 XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(); fontProvider.register("fonts/simhei.ttf"); // 将HTML转换成PDF XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(html.getBytes()), null, Charset.forName("UTF-8"), fontProvider); ``` 4. 最后,关闭文件流并保存PDF文件。 ```java document.close(); writer.close(); ``` 完整的代码示例: ```java import com.itextpdf.text.Document; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.tool.xml.XMLWorkerFontProvider; import com.itextpdf.tool.xml.XMLWorkerHelper; import org.apache.commons.io.FileUtils; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileOutputStream; import java.nio.charset.Charset; public class HtmlToPdfConverter { public static void main(String[] args) throws Exception { String htmlFile = "input.html"; // HTML文件路径 String outputFile = "output.pdf"; // PDF文件路径 Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile)); document.open(); // 读取HTML文件 String html = FileUtils.readFileToString(new File(htmlFile), "UTF-8"); // 创建字体 XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(); fontProvider.register("fonts/simhei.ttf"); // 将HTML转换成PDF XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(html.getBytes()), null, Charset.forName("UTF-8"), fontProvider); document.close(); writer.close(); } } ``` 注意:在转换HTML文件时,需要注意HTML中的CSS样式和图片等外部资源的引用路径。同时,iText对HTML的解析有一定限制,不支持所有的HTML标签和CSS样式,需要根据具体情况进行调整。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值