maven依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ttxit</groupId>
<artifactId>artifact</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.jodd</groupId>
<artifactId>jodd-madvoc</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>org.jodd</groupId>
<artifactId>jodd-http</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>org.jodd</groupId>
<artifactId>jodd-core</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.9</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.10</version>
<configuration>
<encoding>UTF-8</encoding>
<scanIntervalSeconds>10</scanIntervalSeconds>
<stopKey>foo</stopKey>
<stopPort>9999</stopPort>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0.2</version>
<configuration>
</configuration>
</plugin>
</plugins>
</build>
</project>
pdfutils源码
package com.ttxit.util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.log4j.Logger;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
public class PdfUtils {
private static final Logger logger = Logger.getLogger(PdfUtils.class);
static String PDF = "/Users/shicheng/Desktop/2_d.pdf";
public static final String NAME = "STSong-Light";
public static final String ENCODE = "UniGB-UCS2-H";
public static final int SIZE = 12;
/**
* 生成添加文字数据流
*
* @param text
* 输入信息
* @param width
* 宽度
* @param height
* 高度
* @return 数据流
*/
public static byte[] addText(String text, int width, int height) {
ByteArrayOutputStream byteArrayOutputStream = null;
PdfReader pdfReader = null;
PdfStamper pdfStamper = null;
PdfContentByte pdfContentByte = null;
try {
BaseFont baseFont = BaseFont.createFont(NAME, ENCODE, BaseFont.NOT_EMBEDDED);
InputStream inputStream = new FileInputStream(new File(PDF)); // 读取默认pdf文件
pdfReader = new PdfReader(inputStream); // 加载文件到pdf引擎
byteArrayOutputStream = new ByteArrayOutputStream();
pdfStamper = new PdfStamper(pdfReader, byteArrayOutputStream); // 加载模板
pdfContentByte = pdfStamper.getOverContent(1); // 获取顶部
pdfContentByte.beginText(); // 插入文字信息
pdfContentByte.setFontAndSize(baseFont, SIZE);
BaseColor baseColor = new BaseColor(0, 0, 0);
pdfContentByte.setColorFill(baseColor);
pdfContentByte.setTextMatrix(width, height); // 设置文字在页面中的坐标
pdfContentByte.showText(text);
pdfContentByte.endText();
} catch (Exception e) {
if (logger.isDebugEnabled()) {
logger.error("addText(String, int, int) - Exception e=" + e); //$NON-NLS-1$
}
} finally {
try {
pdfStamper.close();
pdfReader.close();
byteArrayOutputStream.close();
} catch (Exception _e) {
if (logger.isDebugEnabled()) {
logger.error("addText(String, int, int) - Exception e=" + _e); //$NON-NLS-1$
}
}
}
return byteArrayOutputStream.toByteArray();
}
}
action源码
package com.ttxit.action;
import org.apache.log4j.Logger;
import com.ttxit.util.PdfUtils;
import jodd.madvoc.meta.Action;
import jodd.madvoc.meta.In;
import jodd.madvoc.meta.MadvocAction;
import jodd.madvoc.meta.Out;
import jodd.madvoc.result.RawData;
import jodd.util.MimeTypes;
@MadvocAction
public class ImageAction {
private static final Logger logger = Logger.getLogger(ImageAction.class);
@Out
String json = "hello";
@In
int width;
@In
int height;
@Action(value = "/image/brokenhearted_card", method = Action.GET)
public RawData hello() {
if (logger.isDebugEnabled()) {
logger.debug("hello() - start"); //$NON-NLS-1$
}
RawData returnRawData = new RawData(PdfUtils.addText("AAA", width, height), MimeTypes.lookupMimeType("pdf"));
if (logger.isDebugEnabled()) {
logger.debug("hello() - end"); //$NON-NLS-1$
}
return returnRawData;
}
}