【发布时间】:2018-02-01 02:42:28
【问题描述】:
我想修改 html 文件以将其转换为 pdf。
目前我使用“ITextRenderer”将 html 文件转换为 pdf。
目前:
OutputStream out = new FileOutputStream(htmlFileOutPutPath);
//Flying Saucer
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(htmlFilePath);
renderer.layout();
renderer.createPDF(out);
out.close();
//This success!! html file to pdf generated!
1-但后来我需要在将其生成为pdf之前修改html文件,为此我认为提取html文件内容并转换为字符串,然后我替换字符串html上的一些文本:
public String htmlFileToString() throws IOException {
StringBuilder contentBuilder = new StringBuilder();
String path = "C:/Users/User1/Desktop/to_pdf_replace.html";
BufferedReader in = new BufferedReader(new FileReader(path));
String str;
while ((str = in.readLine()) != null) {
contentBuilder.append(str);
}
in.close();
String content = contentBuilder.toString();
return content;
}
2- 然后从 html 中替换字符串中的标签
public String replaceHTMLcontent(String strSource)
{
String name = "Ana";
String age = "23";
String html = strSource;
strSource = strSource.replace("##Name##", name);
strSource = strSource.replace("##Age##", age);
//## ## -> are my html custom tags to replace
return strSource;
}
主要:
public static void main(String[] args) {
String stringFromHtml = new DocumentBLL().htmlFileToString();
String stringFromHtmlReplaced = new DocumentBLL().replaceHTMLcontent(stringFromHtml );
}
但是现在不知道怎么用html文件的旧html字符串替换新字符串
【问题讨论】:
标签: java string pdf tags document