日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java pdf中文乱码_java转pdf(html转为pdf),解决中文乱码,标签不规范等问题

發(fā)布時(shí)間:2023/12/15 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java pdf中文乱码_java转pdf(html转为pdf),解决中文乱码,标签不规范等问题 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

第一步,下載jar包以及建對(duì)應(yīng)的文件夾。注意pd4ml的jar要選擇pro版本。然后建一個(gè)pd4fonts.properties

里面對(duì)應(yīng)的字體。

SimSun = simsun.ttf

前面為變量名,后面要對(duì)應(yīng)你下載好的字體。網(wǎng)上都有各種字體下載。相應(yīng)步驟做完了,做完后的文件夾如圖格式都有了!

注意要引入圖片中對(duì)應(yīng)的jar下面的三個(gè)jar包到項(xiàng)目中去。

以下為從文件讀取到數(shù)據(jù)再作導(dǎo)出功能。

import java.awt.Insets;

import java.io.BufferedInputStream;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.StringReader;

import java.net.MalformedURLException;

import java.security.InvalidParameterException;

import org.zefer.pd4ml.PD4Constants;

import org.zefer.pd4ml.PD4ML;

public class Test {

protected int topValue = 10;

protected int leftValue = 20;

protected int rightValue = 10;

protected int bottomValue = 10;

protected int userSpaceWidth = 1300;

/**

* @param args

*/

public static void main(String[] args) {

try {

Test jt = new Test();

//此處填寫你的html文件

String html = readFile("/Users/wangchen/Desktop/370fx2.html", "UTF-8");

//此處填寫你下載的地方

jt.doConversion2(html, "/Users/wangchen/Desktop/370fx2.pdf");

} catch (Exception e) {

e.printStackTrace();

}

}

public void doConversion2(String htmlDocument, String outputPath)

throws InvalidParameterException, MalformedURLException,

IOException {

PD4ML pd4ml = new PD4ML();

pd4ml.enableDebugInfo();

pd4ml.setHtmlWidth(userSpaceWidth);

pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));

pd4ml.setPageInsetsMM(new Insets(topValue, leftValue, bottomValue,

rightValue));

//此處的classPath注意一定要獲取到你放fonts的文件夾。他需要獲取到你下載的字體

String classPath = Test.class.getResource("/")+"fonts";

pd4ml.useTTF(classPath, true);

pd4ml.setDefaultTTFs("SimSun", "SimSun", "SimSun");

ByteArrayOutputStream baos = new ByteArrayOutputStream();

pd4ml.render(new StringReader(htmlDocument), baos);

baos.close();

File output = new File(outputPath);

java.io.FileOutputStream fos = new java.io.FileOutputStream(output);

fos.write(baos.toByteArray());

fos.close();

}

private final static String readFile(String path, String encoding)

throws IOException {

File f = new File(path);

FileInputStream is = new FileInputStream(f);

BufferedInputStream bis = new BufferedInputStream(is);

ByteArrayOutputStream fos = new ByteArrayOutputStream();

byte buffer[] = new byte[2048];

int read;

do {

read = is.read(buffer, 0, buffer.length);

if (read > 0) {

fos.write(buffer, 0, read);

}

} while (read > -1);

fos.close();

bis.close();

is.close();

return fos.toString(encoding);

}

}

如上你就可以下載將html轉(zhuǎn)為pdf了。任意文本也可以轉(zhuǎn)為pdf,經(jīng)測(cè)試,可用

附件如下:https://pan.baidu.com/s/1wSvBM6Kti4IpI9IlDaycew

以下為瀏覽器下載pdf的工具類。直接調(diào)用紅色的方法即可。htmlDocument 為你要導(dǎo)出的數(shù)據(jù),response為該次請(qǐng)求的響應(yīng)體,fileName為下載的名字

package com.ccb.common.utils;

import org.apache.commons.io.output.ByteArrayOutputStream;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletResponse;

import java.awt.*;

import java.io.*;

import java.security.InvalidParameterException;

import org.zefer.pd4ml.PD4Constants;

import org.zefer.pd4ml.PD4ML;

/**

* @author caihong

* @time 2019/1/28

*/

public class PDFUtil {

private static String PDF_TYPE="application/pdf";

private static String classpath=PDFUtil.class.getResource("/").getPath();

protected static int topValue = 10;

protected static int leftValue = 20;

protected static int rightValue = 10;

protected static int bottomValue = 10;

protected static int userSpaceWidth = 1300;

public static void pdf4htmlToPdf(String htmlDocument,HttpServletResponse response, String filename)throws InvalidParameterException,

IOException {

PD4ML pd4ml = new PD4ML();

pd4ml.enableDebugInfo();

pd4ml.setHtmlWidth(userSpaceWidth);

pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));

pd4ml.setPageInsetsMM(new Insets(topValue, leftValue, bottomValue,rightValue));

pd4ml.useTTF(classpath+"fonts", true);

pd4ml.setDefaultTTFs("SimSun", "SimSun", "SimSun");

ByteArrayOutputStream baos = new ByteArrayOutputStream();

pd4ml.render(new StringReader(htmlDocument), baos);

baos.close();

renderPdf(response,baos.toByteArray(),filename);

}

public static void renderPdf(HttpServletResponse response, final byte[] bytes, final String filename) {

initResponseHeader(response, PDF_TYPE);

setFileDownloadHeader(response, filename, ".pdf");

if (null != bytes) {

try {

response.getOutputStream().write(bytes);

response.getOutputStream().flush();

} catch (IOException e) {

throw new IllegalArgumentException(e);

}

}

}

/**

* 分析并設(shè)置contentType與headers.

*/

private static HttpServletResponse initResponseHeader(HttpServletResponse response, final String contentType, final String... headers) {

// 分析headers參數(shù)

String encoding = "utf-8";

boolean noCache = true;

for (String header : headers) {

String headerName = StringUtils.substringBefore(header, ":");

String headerValue = StringUtils.substringAfter(header, ":");

if (StringUtils.equalsIgnoreCase(headerName, "utf-8")) {

encoding = headerValue;

} else if (StringUtils.equalsIgnoreCase(headerName, "no-cache")) {

noCache = Boolean.parseBoolean(headerValue);

} else {

throw new IllegalArgumentException(headerName + "不是一個(gè)合法的header類型");

}

}

// 設(shè)置headers參數(shù)

String fullContentType = contentType + ";charset=" + encoding;

response.setContentType(fullContentType);

if (noCache) {

// Http 1.0 header

response.setDateHeader("Expires", 0);

response.addHeader("Pragma", "no-cache");

// Http 1.1 header

response.setHeader("Cache-Control", "no-cache");

}

return response;

}

/**

* 設(shè)置讓瀏覽器彈出下載對(duì)話框的Header.

* @param

*/

public static void setFileDownloadHeader(HttpServletResponse response, String fileName, String fileType) {

try {

// 中文文件名支持

String encodedfileName = new String(fileName.getBytes("GBK"), "ISO8859-1");

response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedfileName + fileType + "\"");

} catch (UnsupportedEncodingException e) {

}

}

private final static String readFile(String path, String encoding)

throws IOException {

File f = new File(path);

FileInputStream is = new FileInputStream(f);

BufferedInputStream bis = new BufferedInputStream(is);

java.io.ByteArrayOutputStream fos = new java.io.ByteArrayOutputStream();

byte buffer[] = new byte[2048];

int read;

do {

read = is.read(buffer, 0, buffer.length);

if (read > 0) {

fos.write(buffer, 0, read);

}

} while (read > -1);

fos.close();

bis.close();

is.close();

return fos.toString(encoding);

}

}

自己完成controller,以及mapping映射后,注意要用get請(qǐng)求

http://localhost:8080/api/img/exportPdf?htmlDocument=12%E7%9A%84%E5%8F%91%E9%A1%BA%E4%B8%B0%E9%98%BF%E6%96%AF%E9%A1%BF%E5%8F%91%E9%80%81%E5%88%B0%E5%8F%91%E9%80%81%E5%A4%A7&fileName=123

便可以下載pdf了

總結(jié)

以上是生活随笔為你收集整理的java pdf中文乱码_java转pdf(html转为pdf),解决中文乱码,标签不规范等问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。