输出 pdf

jar 包 :core-renderer.jar  iText-2.0.8.jar   iTextAsian.jar

方式1:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import com.lowagie.text.pdf.BaseFont;

public class pdf {
 public static void main(String[] args) throws FileNotFoundException {
     List<String> list = new ArrayList<String>();
     for (int i = 0; i < 10; i++) {
         list.add("test");
    }
      String outputFile = "e:/firstdoc.pdf";
      OutputStream os = new FileOutputStream(outputFile);
     ITextRenderer renderer = new ITextRenderer();
     StringBuffer html = new StringBuffer();
     //组装成符合W3C标准的html文件,否则不能正确解析
     html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
     html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">")
         .append("<head>")
         .append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />")
         .append("<style type=\"text/css\" mce_bogus=\"1\">body {font-family: Arial Unicode MS;}</style>")
         .append("<style type=\"text/css\">img {width: 500px;}</style>")
         .append("<style type=\"text/css\">table {font-size:13px;}</style>")
         .append("</head>")
         .append("<body>");
     html.append("<center>");
     html.append("<div>报表测试</div>");
     for(int i=0;i<list.size();i++) {
         html.append("<div>" + list.get(i) + "</div>");
     }
     html.append("</center>");
     html.append("</body></html>");
     try {
         renderer.setDocumentFromString(html.toString());
//         ITextFontResolver fontResolver = renderer.getFontResolver();
//         fontResolver.addFont("e:/fonts/ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
         // 解决图片的相对路径问题,图片路径必须以file开头
//         renderer.getSharedContext().setBaseURL("file:/" + rootpath);
         renderer.layout();
         renderer.createPDF(os);
         os.close();
         System.out.println("ok");
     } catch (Exception e) {
         e.printStackTrace();
     }
}
}

方式2

import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class pdf2 {
 public static void main(String[] args) throws FileNotFoundException, Exception {
     List<String> list = new ArrayList<String>();
     for (int i = 0; i < 10; i++) {
         list.add("test");
    }
      String outputFile = "e:/firstdoc.pdf";
      OutputStream ops = new FileOutputStream(outputFile);
      Document document = new Document();
      PdfWriter.getInstance(document, ops);
      document.open();
      // set chinese font
      BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
      Font f2 = new Font(bfChinese, 2, Font.NORMAL);
      Font f6 = new Font(bfChinese, 6, Font.NORMAL);
      Font f8 = new Font(bfChinese, 8, Font.NORMAL);
      Font f10 = new Font(bfChinese, 10, Font.NORMAL);
      Font f12 = new Font(bfChinese, 12, Font.BOLD);
//      int rowNumer = 0;
//      PdfPTable table = new PdfPTable(2);
//      table.setWidthPercentage(80);
//    table.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
//     PdfPCell titleCell = new PdfPCell();
//     titleCell.setBackgroundColor(new Color(213, 141, 69));
//     titleCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
//     titleCell.setPhrase(new Paragraph("人口指标", f8));
//     table.addCell(titleCell);
//     titleCell.setPhrase(new Paragraph("人口指标2", f8));
//     table.addCell(titleCell);
//     document.add(table);
      PdfPTable table = new PdfPTable(3);
      PdfPCell cell;
      cell = new PdfPCell(new Phrase("Cell with colspan 3"));
      cell.setColspan(3);
      table.addCell(cell);
      cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
      cell.setColspan(2);
      table.addCell(cell);
      table.addCell("row 1; cell 1");
      table.addCell("row 1; cell 2");
      table.addCell("row 2; cell 1");
      table.addCell("row 2; cell 2");  

      document.add(table);
      document.close();
      ops.close();
     System.out.println("ok");
 }
}
时间: 2024-08-11 05:31:52

输出 pdf的相关文章

php 使用tcpdf库输出pdf

试了几种其他的插件,总是搞不定编码问题,用这个成功了 <?php require_once('./tcpdf/tcpdf.php'); //引入库文件 //实例化 $pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false); // 设置文档信息 $pdf->SetCreator('Helloweba'); $pdf->SetAuthor('yueguangguang'); $pdf->SetTitle('Welcome to he

django “如何”系列9:三合一:利用遗留的数据库、输出csv和输出pdf

如何集成遗留的数据库 django在适合开发新应用的同时,可以可以集成以前遗留的数据库,下面是如何集成一个已经存在的数据库的流程. 给定你的数据库的参数 你需要告诉django你的数据库连接参数以及数据库的名字,你可以通过修改django配置文件的DATABASES中的default连接来给定你的数据库参数 NAME ENGINE USER PASSWORD HOST PORT 自动生成模型 django自带了一个工具inspectdb,这个工具可以通过反溯一个已经存在的数据库来创建一个模型 p

Servlet输出PDF文档方法

概述 Java Servlet 编程可以很方便地将 HTML 文件发送到客户端的 Web 浏览器.然而许多站点还允许访问非 HTML 格式的文档,包括 Adobe PDF.Microsoft Word 和 Micorsoft Excel 等.事实上这些非 HTML 格式只要能用 MIME 类型表示,就可以利用 servlet 来发送.本文将以 PDF为例,向你介绍如何使用 servlet 传送非 HTML 格式文件及该文件是如何在服务器端用Java生成的. 你只要将文件写到servlet 的输出

Latex输出pdf支持中文那些事

今天闹腾了一上午,中午搞定了,如何成功将 .tex 文件输出成 .pdf 的时候支持中文,过程有点复杂,为什么呢,因为我还想学 Latex ,所以把所有的包就一次性搞好了. 首先是去下载并安装 Tex Live 2018 , 3.0+G: 清华镜像:https://mirrors.tuna.tsinghua.edu.cn/CTAN/systems/texlive/Images/texlive2018-20180414.iso 然后下载并安装 Tex Studio ,这就是一个前端,本来我想用VS

OAF中输出PDF报表文件超链接

OAF中输出PDF报表文件超链接解决方案 解决思路 获取PDF文件的输出流 获取输出流之后将其转换为BLOB 将转化后的BLOB存储在附件表fnd_blobs中 构造该BLOB文件的URL 将URL存储在客户化表里形成一条唯一的记录,返回唯一ID FORM通过返回得唯一ID找到URL,调用方法,在浏览器打开URL 技术要点 使用TemplateHelper.processTemplate方法生成目标PDF的InputStream流 调用程序包将InputStream传入数据库中的BLOB字段中,

Jupyter Notebook通过latex输出pdf

主要步骤 1.将ipynb编译成tex ipython nbconvert --to latex Example.ipynb 2. 修改tex,增加中文支持 在\documentclass{article}后面插入 \usepackage{fontspec, xunicode, xltxtra} \setmainfont{Microsoft YaHei} \usepackage{ctex} 3. 编译tex,生成pdf xelatex Example.tex 如果插入图片 需要把图片downlo

itextpdf JAVA 输出PDF文档

使用JAVA生成PDF的时候,还是有些注意事项需要处理的. 第一.中文问题,默认的itext是不支持中文的,想要支持,需要做些处理. 1.直接引用操作系统的中文字体库支持,由于此方案限制性强,又绑定了操作系统,所以此处不做实现,有兴趣可在网上搜索看看. 2.引用itext-asian.jar包的字体支持,代码稍后上. 第二.表格中的设置,特别是上中下,左中右,不同的对象有不同的枚举实现,刚入手很容易混淆.其外是前景色,背景色,表格颜色等等. 第三.输出图片,很容易报错.. package com

c# 中crystal report输出PDF文件

工程中引入以下crystal report的类库crystaldecisions.crystalreports.enginecrystaldecisions.reportsourcecrystaldecisions.sharedcrystaldecisions.web 将CrystalReportViewer控件添加入页面.同时为他的初始化添加代码System.Data.DataTable dt = ....;//这里写得到数据表的方法.可以根据自己的爱好写方法,但是一定要得到DataTable

java输出pdf

package snake; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowag