|
|
现在来看看将原作者的一片博客通过iText转换为pdf文件,该博客htmlDemo.html文件如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="zh-CN"><head profile="http://gmpg.org/xfn/11"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>/dev/null 2&gt;&amp;1 详解 | micmiu - 大大的技术 | 小小的生活</title><link rel="stylesheet" type="text/css" media="all"href="http://www.micmiu.com/wp-content/themes/zbench/style.css" /><link rel='shortlink' href='http://www.micmiu.com/?p=814' /></head><body class="single single-post postid-814 single-format-standard"><div class="entry"><p>我们在shell 脚本命令中会经常看到类似这样内容:<span style="color: #ff0000;">/dev/null2&gt;&amp;1</span>,这条命令的意思是将标准输出和错误输出全部重定向到 /dev/null 空设置中,也就是将产生的所有信息丢弃。</p><p><span style="color: #0000ff;">[一]、命令的解释</span></p><p><span style="color: #ff0000;"> &gt;/dev/null 2&gt;&amp;1</span>可以拆分开易于理解:</p><ol><li><span style="line-height: 22px;"><spanstyle="color: #ff0000;">&gt;</span>&nbsp;:代表重定向到哪里,例如:echo“micmiu.com” &gt; /home/michaeltest.txt<br /> </span></li><li><span style="line-height: 22px;"><spanstyle="color: #ff0000;">/dev/null</span>&nbsp;:代表空设备文件<br /> </span></li><li><span style="line-height: 22px;"><spanstyle="color: #ff0000;">2&gt;</span>&nbsp;:表示stderr标准错误<br /> </span></li><li><span style="line-height: 22px;"><spanstyle="color: #ff0000;">&amp;</span>&nbsp;:表示等同于的意思,2&gt;&amp;1,表示2的输出重定向等同于1<br /></span></li><li><span style="line-height: 22px;"><spanstyle="color: #ff0000;">1</span>&nbsp;:表示stdout标准输出,系统默认值是1,所以”&gt;/dev/null”等同于“1&gt;/dev/null”<br /> </span></li></ol><p>故:”&gt;/dev/null 2&gt;&amp;1″ 也可以写成 “1&gt;/dev/null&nbsp;2&gt;&amp;1″</p><p><span style="color: #0000ff;">[二]、命令的执行过程</span></p><p><span style="color: #ff0000;">1&gt;/dev/null</span>:首先表示标准输出重定向到空设备文件,也就是不输出任何信息到终端,说白了就是不显示任何信息。<br /> <spanstyle="color: #ff0000;">2&gt;&amp;1</span>:接着,标准错误输出重定向到标准输出,因为之前标准输出已经重定向到了空设备文件,所以标准错误输出也重定向到空设备文件。</p><p>本文介绍到此结束@<a href="http://www.micmiu.com/">Michael Sun</a>.</p></div></body></html>
该博客IE浏览器解析截图如下:

转换代码如下:
public static void main(String[] args) throws DocumentException,IOException {InputStream is = Test4.class.getClassLoader().getResourceAsStream("htmlDemo.html");String pdfPath1 = "d:/test4_1.pdf";// 方式一:直接进行转换(其中又包含两种方式)parseHtml2PdfFile(pdfPath1, is);is = Test4.class.getClassLoader().getResourceAsStream("htmlDemo.html");String pdfPath2 = "d:/test4_2.pdf";// 方式二:转换为pdf文件元素再进行转换(看起来类似于xml的SAX解析模型,这种方式好处主要在于可以自由追加其它内容)parseHtml2PdfElement(pdfPath2, is);}private static void parseHtml2PdfFile(String pdfPath, InputStream is)throws DocumentException, IOException {Document doc = new Document(PageSize.A4);PdfWriter pdfWriter = PdfWriter.getInstance(doc, new FileOutputStream(pdfPath));pdfWriter.setViewerPreferences(PdfWriter.HideToolbar);doc.open();BaseFont bf = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);Font chaFont = new Font(bf, 12, Font.BOLD, BaseColor.BLACK);Font secFont = new Font(bf, 10, Font.BOLDITALIC, BaseColor.BLUE);Chapter chapter = new Chapter(new Paragraph("html转pdf测试", chaFont), 1);Section section = chapter.addSection(new Paragraph("/dev/null 2>&1 详解",secFont));section.setBookmarkOpen(false);section.setIndentation(10);section.setIndentationLeft(10);section.setNumberStyle(Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT);section.add(Chunk.NEWLINE);section.setNumberDepth(10);// 不知道干什么用section.setBookmarkTitle("基本信息");// 设置节书签标识doc.add(chapter);InputStreamReader isr = new InputStreamReader(is, "UTF-8");// 方式一:直接转换// XMLWorkerHelper.getInstance().parseXHtml(pdfWriter, doc, isr);// 方式二:自定义参数进行转换HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);htmlContext.charSet(Charset.forName("UTF-8"));htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());CSSResolver cssResolver = XMLWorkerHelper.getInstance().getDefaultCssResolver(true);Pipeline<?> pipeline = new CssResolverPipeline(cssResolver,new HtmlPipeline(htmlContext, new PdfWriterPipeline(doc,pdfWriter)));XMLWorker worker = new XMLWorker(pipeline, true);XMLParser p = new XMLParser();p.addListener(worker);p.parse(isr);p.flush();doc.close();}private static void parseHtml2PdfElement(String pdfPath, InputStream is)throws DocumentException, IOException {Document doc = new Document(PageSize.A4);PdfWriter pdfWriter = PdfWriter.getInstance(doc, new FileOutputStream(pdfPath));pdfWriter.setViewerPreferences(PdfWriter.HideToolbar);doc.open();BaseFont bf = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);Font chaFont = new Font(bf, 12, Font.BOLD, BaseColor.BLACK);Font secFont = new Font(bf, 10, Font.BOLDITALIC, BaseColor.BLUE);Font textFont = new Font(bf, 8, Font.NORMAL, BaseColor.RED);Chapter chapter = new Chapter(new Paragraph("html转pdf元素便于追加其它内容",chaFont), 1);Section section = chapter.addSection(new Paragraph("/dev/null 2>&1 详解",secFont));section.setBookmarkOpen(false);section.setIndentation(10);section.setIndentationLeft(10);section.setNumberStyle(Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT);final List<Element> pdfEleList = new ArrayList<Element>();ElementHandler eleHandler = new ElementHandler() {@Overridepublic void add(Writable writable) {if (writable instanceof WritableElement) {pdfEleList.addAll(((WritableElement) writable).elements());}}};InputStreamReader isr = new InputStreamReader(is, "UTF-8");XMLWorkerHelper.getInstance().parseXHtml(eleHandler, isr);// System.out.println(pdfEleList);List<Element> eleList = new ArrayList<Element>();for (Element e : pdfEleList) {if (e instanceof LineSeparator|| e instanceof WritableDirectElement) {continue;}eleList.add(e);}// System.out.println(eleList);section.addAll(eleList);section.add(Chunk.NEWLINE);section = chapter.addSection(new Paragraph("继续添加章节", secFont));section.setBookmarkOpen(false);section.setIndentation(10);section.setIndentationLeft(10);section.setNumberStyle(Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT);section.add(new Chunk("便于追加其它内容", textFont));doc.add(chapter);doc.close();}
生成的pdf文件截图:

 |
|