有需求是要把html网页上的内容直接转成pdf,怎么实现呢?

解决方案 »

  1.   

    pdf的浏览器有这个功能啊,可以直接转换的。我用过6.0版本的
      

  2.   

    我记得firefox还有这么一个扩展的,其实就是把当前url发到另一个服务器上处理后传回来。
      

  3.   

    可以啊
    好想要一个jar包一、前言  在企业的信息系统中,报表处理一直占比较重要的作用,本文将介绍一种生成PDF报表的Java组件--iText。通过在服务器端使用Jsp或JavaBean生成PDF报表,客户端采用超级连接显示或下载得到生成的报表,这样就很好的解决了B/S系统的报表处理问题。  二、iText简介  iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。  iText的安装非常方便,在http://www.lowagie.com/iText/download.html - download 网站上下载iText.jar文件后,只需要在系统的CLASSPATH中加入iText.jar的路径,在程序中就可以使用iText类库了。  三、建立第一个PDF文档  用iText生成PDF文档需要5个步骤:  ①建立com.lowagie.text.Document对象的实例。
    Document document = new Document();   ②建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
    PDFWriter.getInstance(document, new FileOutputStream("Helloworld.PDF"));   ③打开文档。
    document.open();   ④向文档中添加内容。
    document.add(new Paragraph("Hello World"));   ⑤关闭文档。
    document.close();   通过上面的5个步骤,就能产生一个Helloworld.PDF的文件,文件内容为"Hello World"。  建立com.lowagie.text.Document对象的实例  com.lowagie.text.Document对象的构建函数有三个,分别是:
    public Document();
    public Document(Rectangle pageSize);
    public Document(Rectangle pageSize,
    int marginLeft,
    int marginRight,
    int marginTop,
    int marginBottom);   构建函数的参数pageSize是文档页面的大小,对于第一个构建函数,页面的大小为A4,同Document(PageSize.A4)的效果一样;对于第三个构建函数,参数marginLeft、marginRight、marginTop、marginBottom分别为左、右、上、下的页边距。  通过参数pageSize可以设定页面大小、面背景色、以及页面横向/纵向等属性。iText定义了A0-A10、AL、LETTER、HALFLETTER、_11x17、LEDGER、NOTE、B0-B5、ARCH_A-ARCH_E、FLSA 和FLSE等纸张类型,也可以通过Rectangle pageSize = new Rectangle(144, 720);自定义纸张。通过Rectangle方法rotate()可以将页面设置成横向。  书写器(Writer)对象  一旦文档(document)对象建立好之后,需要建立一个或多个书写器(Writer)对象与之关联。通过书写器(Writer)对象可以将具体文档存盘成需要的格式,如com.lowagie.text.PDF.PDFWriter可以将文档存成PDF文件,com.lowagie.text.html.HtmlWriter可以将文档存成html文件。  设定文档属性  在文档打开之前,可以设定文档的标题、主题、作者、关键字、装订方式、创建者、生产者、创建日期等属性,调用的方法分别是:
    public boolean addTitle(String title)
    public boolean addSubject(String subject)
    public boolean addKeywords(String keywords)
    public boolean addAuthor(String author)
    public boolean addCreator(String creator)
    public boolean addProducer()
    public boolean addCreationDate()
    public boolean addHeader(String name, String content)   其中方法addHeader对于PDF文档无效,addHeader仅对html文档有效,用于添加文档的头信息。
    当新的页面产生之前,可以设定页面的大小、书签、脚注(HeaderFooter)等信息,调用的方法是:
    public boolean setPageSize(Rectangle pageSize)
    public boolean add(Water water)
    public void removeWater()
    public void setHeader(HeaderFooter header)
    public void resetHeader()
    public void setFooter(HeaderFooter footer)
    public void resetFooter()
    public void resetPageCount()
    public void setPageCount(int pageN)    如果要设定第一页的页面属性,这些方法必须在文档打开之前调用。  对于PDF文档,iText还提供了文档的显示属性,通过调用书写器的setViewerPreferences方法可以控制文档打开时Acrobat Reader的显示属性,如是否单页显示、是否全屏显示、是否隐藏状态条等属性。  另外,iText也提供了对PDF文件的安全保护,通过书写器(Writer)的setEncryption方法,可以设定文档的用户口令、只读、可打印等属性。  添加文档内容  所有向文档添加的内容都是以对象为单位的,如Phrase、Paragraph、Table、Graphic对象等。比较常用的是段落(Paragraph)对象,用于向文档中添加一段文字。
      

  4.   

    1、如果你想在程序中实现生成pdf,那么使用itext,或者ireport,建议使用后者
    2、如果要把现成的网页文件转换成pdf,那么可以使用5D-PDF,或者adobe自带的这类虚拟打印机软件
      

  5.   

    另外,如果在程序中调用执行,可以使用一个html2pdf的插件,在程序中用cmd命令执行,支持多个html文件合成一个pdf文件
      

  6.   

    html ------------>word-------------->pdf
    adobe acrobat7.0 support word------------>pdf
      

  7.   

    我在用itext的,程序里可以实现生成pdf,但是排版很难,一个个赋值换行很麻烦,而且格式也排不好。
    想问问有谁试过用这个直接把页面上显示的东西按照页面的格式转成pdf呢?能给个例子更好了。
    public void creatPdf(String html) throws DocumentException, IOException{
    Document document = new Document();  
    PdfWriter.getInstance(document, new FileOutputStream("d:/Helloworld.PDF"));  
    document.open(); 

    BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",    
                BaseFont.NOT_EMBEDDED);    
                   
    Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);   
    //
    if(html   ==null)     html   ="";   
    html   =   html.replaceAll("\\<[^\\>]*\\>",",");   
    html   =   html.replaceAll("&nbsp;",",");   
    html   =   html.replaceAll("[,]+",",");   
      if   (html.startsWith(","))   
      html   =   html.substring(1);   
      if   (html.endsWith(","))   
      html   =   html.substring(0,html.length()-1);   
      System.out.println(html);   
      String[]   strResult   =   html.split(",");   
      
      int[]   nTitleItemWidth={50,50};      PdfPTable   datatable   =   new   PdfPTable(2);   
      datatable.setWidths(nTitleItemWidth);   
      datatable.getDefaultCell().setPadding(3);   
      datatable.setWidthPercentage(100);   //   percentage   
      for   (int   i   =   0   ;i<strResult.length   ;   i++){   
      PdfPCell   pCTitle=new   PdfPCell(new   Phrase(strResult[i],FontChinese));   
      pCTitle.setColspan(1);   
      pCTitle.setBorderWidth(1);   
      pCTitle.setHorizontalAlignment(0);   
      pCTitle.setFixedHeight(20);   
      datatable.addCell(pCTitle);   
                      i++;     
        }
    //
    document.add(datatable);  
    document.close();   }我目前这个方法把页面上整个table的innerHTML作为参数传进去,生成的pdf很怪,只有部分字段,而且排版也不大对。这里面大部分是网上找的程序,也不懂有些方法什么意思。
    谁有这方面经验,给段代码参考下吧最好从页面传什么到后台怎么处理代码都有
      

  8.   

    自己拼格式生成
    Document document = new Document();  
    PdfWriter.getInstance(document, new FileOutputStream("d:/test.PDF"));  
    document.open();  
      
    //中文字符
      BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",    
                BaseFont.NOT_EMBEDDED);    
                   
    Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);   
    Font FontHead = new Font(bfChinese, 18, Font.NORMAL);          
      //
    //int[]   nTitleItemWidth={50,50};      PdfPTable   datatable   =   new   PdfPTable(4);  //建立列数为4的表格 
      //datatable.setWidths(nTitleItemWidth);   
      //datatable.getDefaultCell().setPadding(3);   
      datatable.setWidthPercentage(100);   //   percentage   
    PdfPCell   pCTitle=new   PdfPCell(new   Phrase("你好",FontHead));  
      //pCTitle.setHorizontalAlignment(arg0)
      pCTitle.setColspan(4);   //占4个列
      pCTitle.setBorderWidth(1);   //边框宽度设置为1
      pCTitle.setHorizontalAlignment(0);   
      pCTitle.setFixedHeight(20);   
      datatable.addCell(pCTitle);  
      
      PdfPCell pdfCell=new   PdfPCell(new   Phrase("你好:",FontChinese));      
      pdfCell.setColspan(1);   
      pdfCell.setBorderWidth(1);   
      pdfCell.setHorizontalAlignment(0);   
      pdfCell.setFixedHeight(20);   
      datatable.addCell(pdfCell);  
      
      pdfCell=new   PdfPCell(new   Phrase("你好",FontChinese));   
      pdfCell.setColspan(1);   
      pdfCell.setBorderWidth(1);   
      pdfCell.setHorizontalAlignment(0);   
      pdfCell.setFixedHeight(20);   
      datatable.addCell(pdfCell); 
      
      pdfCell=new   PdfPCell(new   Phrase("你好:",FontChinese));   
      pdfCell.setColspan(1);   
      pdfCell.setBorderWidth(1);   
      pdfCell.setHorizontalAlignment(0);   
      pdfCell.setFixedHeight(20);   
      datatable.addCell(pdfCell); 
      pdfCell=new   PdfPCell(new   Phrase("你好",FontChinese));   
      pdfCell.setColspan(1);   
      pdfCell.setBorderWidth(1);   
      pdfCell.setHorizontalAlignment(0);   
      pdfCell.setFixedHeight(20);   
      datatable.addCell(pdfCell); 
      
      
      pdfCell=new   PdfPCell(new   Phrase("你好:",FontChinese));      
      pdfCell.setColspan(1);   
      pdfCell.setBorderWidth(1);   
      pdfCell.setHorizontalAlignment(0);   
      pdfCell.setFixedHeight(20);   
      datatable.addCell(pdfCell);  
      
      pdfCell=new   PdfPCell(new   Phrase("你好",FontChinese));   
      pdfCell.setColspan(1);   
      pdfCell.setBorderWidth(1);   
      pdfCell.setHorizontalAlignment(0);   
      pdfCell.setFixedHeight(20);   
      datatable.addCell(pdfCell); 
      
      pdfCell=new   PdfPCell(new   Phrase("你好:",FontChinese));   
      pdfCell.setColspan(1);   
      pdfCell.setBorderWidth(1);   
      pdfCell.setHorizontalAlignment(0);   
      pdfCell.setFixedHeight(20);   
      datatable.addCell(pdfCell); 
      pdfCell=new   PdfPCell(new   Phrase("你好",FontChinese));   
      pdfCell.setColspan(1);   
      pdfCell.setBorderWidth(1);   
      pdfCell.setHorizontalAlignment(0);   
      pdfCell.setFixedHeight(20);   
      datatable.addCell(pdfCell); 
      
      //
     document.add(datatable);  
    document.close();  
      

  9.   

    传说中php有一个pdflib 没有用过