diff --git a/dsPay/pom.xml b/dsPay/pom.xml index 7d235d2f..80a998b0 100644 --- a/dsPay/pom.xml +++ b/dsPay/pom.xml @@ -9,4 +9,28 @@ dsPay Archetype - dsPay http://maven.apache.org + + + + org.xhtmlrenderer + core-renderer + R8pre2 + + + + org.docx4j + xhtmlrenderer + 1.0.0 + + + + + com.lowagie + itext + 2.0.8 + + + + + diff --git a/dsPay/src/main/java/HtmlToPdf.java b/dsPay/src/main/java/HtmlToPdf.java deleted file mode 100644 index 2704d1a7..00000000 --- a/dsPay/src/main/java/HtmlToPdf.java +++ /dev/null @@ -1,55 +0,0 @@ -import java.io.File; - -public class HtmlToPdf { - // wkhtmltopdf在系统中的路径 - private static String toPdfTool = "c:/Windows/System32/wkhtmltoimage.exe"; - - /** - * html转pdf - * - * @param srcPath html路径,可以是硬盘上的路径,也可以是网络路径 - * @param destPath pdf保存路径 - * @return 转换成功返回true - */ - public static boolean convert(String srcPath, String destPath) { - File file = new File(destPath); - File parent = file.getParentFile(); - // 如果pdf保存路径不存在,则创建路径 - if (!parent.exists()) { - parent.mkdirs(); - } - StringBuilder cmd = new StringBuilder(); - if (System.getProperty("os.name").indexOf("Windows") == -1) { - // 非windows 系统 - //toPdfTool = Consts.WEB.CONVERSION_PLUGSTOOL_PATH_LINUX; - System.out.println("需要配置在Linux环境中的路径地址!"); - return false; - } - cmd.append(toPdfTool); - cmd.append(" "); - cmd.append(" \""); - cmd.append(srcPath); - cmd.append("\" "); - cmd.append(" "); - cmd.append(destPath); - - System.out.println(cmd); - boolean result = true; - try { - Process proc = Runtime.getRuntime().exec(cmd.toString()); - HtmlToPdfInterceptor error = new HtmlToPdfInterceptor(proc.getErrorStream()); - HtmlToPdfInterceptor output = new HtmlToPdfInterceptor(proc.getInputStream()); - error.start(); - output.start(); - proc.waitFor(); - } catch (Exception e) { - result = false; - e.printStackTrace(); - } - return result; - } - - public static void main(String[] args) { - HtmlToPdf.convert("D:\\dsWork\\dsProject\\dsPay\\Invoice-master\\index.html", "D:\\dsWork\\dsProject\\dsPay\\Invoice-master\\index.html.pdf"); - } -} \ No newline at end of file diff --git a/dsPay/src/main/java/HtmlToPdfInterceptor.java b/dsPay/src/main/java/HtmlToPdfInterceptor.java deleted file mode 100644 index 9499ade7..00000000 --- a/dsPay/src/main/java/HtmlToPdfInterceptor.java +++ /dev/null @@ -1,26 +0,0 @@ -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; - -public class HtmlToPdfInterceptor extends Thread { - private InputStream is; - - public HtmlToPdfInterceptor(InputStream is) { - this.is = is; - } - - @Override - public void run() { - try { - InputStreamReader isr = new InputStreamReader(is, "utf-8"); - BufferedReader br = new BufferedReader(isr); - String line; - while ((line = br.readLine()) != null) { - System.out.println(line); //输出内容 - } - } catch (IOException e) { - e.printStackTrace(); - } - } -} \ No newline at end of file diff --git a/dsPay/src/main/java/HtmltoPDF.java b/dsPay/src/main/java/HtmltoPDF.java new file mode 100644 index 00000000..d1b01618 --- /dev/null +++ b/dsPay/src/main/java/HtmltoPDF.java @@ -0,0 +1,82 @@ +import java.io.File; +import java.io.FileOutputStream; +import java.io.OutputStream; + + +import org.xhtmlrenderer.pdf.ITextFontResolver; +import org.xhtmlrenderer.pdf.ITextRenderer; + +import com.lowagie.text.pdf.BaseFont; + + +public class HtmltoPDF { + + public static void main( String[] args ) throws Exception{ + + +// htmlToPdf(); +// htmlToPdf2(); + htmlToPdf3(); + } + + // 不支持中文 + public static void htmlToPdf() throws Exception + { + String inputFile = "D:/Test/flying.html"; + String url = new File( inputFile ).toURI().toURL().toString(); + String outputFile = "D:/Test/flying.pdf"; + OutputStream os = new FileOutputStream( outputFile ); + ITextRenderer renderer = new ITextRenderer(); + renderer.setDocument( url ); + renderer.layout(); + renderer.createPDF( os ); + os.close(); + } + + // 支持中文 + public static void htmlToPdf2() throws Exception { + String outputFile = "F:/TestDemo/demo_3.pdf"; + OutputStream os = new FileOutputStream(outputFile); + ITextRenderer renderer = new ITextRenderer(); + ITextFontResolver fontResolver = renderer.getFontResolver(); + fontResolver.addFont("C:/Windows/fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); + StringBuffer html = new StringBuffer(); + // DOCTYPE 必需写否则类似于 这样的字符解析会出现错误 + html.append(""); + html.append(""). + append("") + .append("") + .append("") + .append("") + .append(""); + html.append("
支持中文!
"); + html.append(""); + renderer.setDocumentFromString(html.toString()); + // 解决图片的相对路径问题 + // renderer.getSharedContext().setBaseURL("file:/F:/teste/html/"); + renderer.layout(); + renderer.createPDF(os); + System.out.println("======转换成功!"); + os.close(); + } + + public static void htmlToPdf3() throws Exception{ + String inputFile = "F:/TestDemo/test.html"; //本地目录必须html语言否则报错 + String outFile = "F:/TestDemo/test.pdf"; + OutputStream os = null; + os = new FileOutputStream(outFile); + ITextRenderer renderer = new ITextRenderer(); + ITextFontResolver fontResolver = renderer.getFontResolver(); + fontResolver.addFont("C:/Windows/fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); + + String url = new File( inputFile ).toURI().toURL().toString(); + System.out.println("=============url: "+url); + renderer.setDocument(url); + renderer.layout(); + renderer.createPDF(os); + System.out.println("======转换成功!"); + os.close(); + } + + +} \ No newline at end of file diff --git a/dsPay/wkhtmltox/bin/libwkhtmltox.a b/dsPay/wkhtmltox/bin/libwkhtmltox.a deleted file mode 100644 index 69fa1d05..00000000 Binary files a/dsPay/wkhtmltox/bin/libwkhtmltox.a and /dev/null differ diff --git a/dsPay/wkhtmltox/bin/wkhtmltoimage.exe b/dsPay/wkhtmltox/bin/wkhtmltoimage.exe deleted file mode 100644 index ddf2d204..00000000 Binary files a/dsPay/wkhtmltox/bin/wkhtmltoimage.exe and /dev/null differ diff --git a/dsPay/wkhtmltox/bin/wkhtmltopdf.exe b/dsPay/wkhtmltox/bin/wkhtmltopdf.exe deleted file mode 100644 index f2906cd1..00000000 Binary files a/dsPay/wkhtmltox/bin/wkhtmltopdf.exe and /dev/null differ diff --git a/dsPay/wkhtmltox/bin/wkhtmltox.dll b/dsPay/wkhtmltox/bin/wkhtmltox.dll deleted file mode 100644 index f453ff83..00000000 Binary files a/dsPay/wkhtmltox/bin/wkhtmltox.dll and /dev/null differ diff --git a/dsPay/wkhtmltox/include/wkhtmltox/dllbegin.inc b/dsPay/wkhtmltox/include/wkhtmltox/dllbegin.inc deleted file mode 100644 index 4535efcf..00000000 --- a/dsPay/wkhtmltox/include/wkhtmltox/dllbegin.inc +++ /dev/null @@ -1,54 +0,0 @@ -/* -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*- - * vi:set ts=4 sts=4 sw=4 noet : - * - * Copyright 2010-2020 wkhtmltopdf authors - * - * This file is part of wkhtmltopdf. - * - * wkhtmltopdf is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * wkhtmltopdf is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with wkhtmltopdf. If not, see . - */ - -#ifndef __WKHTMLTOPDF_DLLBEGIN__ -#define __WKHTMLTOPDF_DLLBEGIN__ - -#if defined _WIN32 || defined __CYGWIN__ - #ifdef BUILDING_DLL - #define DLL_PUBLIC __declspec(dllexport) - #else - #define DLL_PUBLIC __declspec(dllimport) - #endif - #define DLL_LOCAL -#else - #if __GNUC__ >= 4 - #define DLL_PUBLIC __attribute__ ((visibility("default"))) - #define DLL_LOCAL __attribute__ ((visibility("hidden"))) - #else - #define DLL_PUBLIC - #define DLL_LOCAL - #endif -#endif - -#if defined _WIN32 -#define CALLTYPE __stdcall -#else -#define CALLTYPE -#endif - -#ifdef __cplusplus - #define CAPI(type) extern "C" DLL_PUBLIC type CALLTYPE -#else - #define CAPI(type) DLL_PUBLIC type CALLTYPE -#endif - -#endif /*__WKHTMLTOPDF_DLLBEGIN__*/ diff --git a/dsPay/wkhtmltox/include/wkhtmltox/dllend.inc b/dsPay/wkhtmltox/include/wkhtmltox/dllend.inc deleted file mode 100644 index ae23f3ba..00000000 --- a/dsPay/wkhtmltox/include/wkhtmltox/dllend.inc +++ /dev/null @@ -1,30 +0,0 @@ -/* -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*- - * vi:set ts=4 sts=4 sw=4 noet : - * - * Copyright 2010-2020 wkhtmltopdf authors - * - * This file is part of wkhtmltopdf. - * - * wkhtmltopdf is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * wkhtmltopdf is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with wkhtmltopdf. If not, see . - */ - -#ifdef __WKHTMLTOPDF_DLLBEGIN__ - -#undef __WKHTMLTOPDF_DLLBEGIN__ -#undef DLL_PUBLIC -#undef DLL_LOCAL -#undef CAPI -#undef CALLTYPE - -#endif /*__WKHTMLTOPDF_DLLBEGIN__*/ diff --git a/dsPay/wkhtmltox/include/wkhtmltox/image.h b/dsPay/wkhtmltox/include/wkhtmltox/image.h deleted file mode 100644 index 6b1f9102..00000000 --- a/dsPay/wkhtmltox/include/wkhtmltox/image.h +++ /dev/null @@ -1,76 +0,0 @@ -/* -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*- - * vi:set ts=4 sts=4 sw=4 noet : - * - * Copyright 2010-2020 wkhtmltopdf authors - * - * This file is part of wkhtmltopdf. - * - * wkhtmltopdf is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * wkhtmltopdf is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with wkhtmltopdf. If not, see . - */ - -#ifndef __IMAGE_H__ -#define __IMAGE_H__ - -#ifdef BUILDING_WKHTMLTOX -#include "dllbegin.inc" -#else -#include -#endif - -struct wkhtmltoimage_global_settings; -typedef struct wkhtmltoimage_global_settings wkhtmltoimage_global_settings; - -struct wkhtmltoimage_converter; -typedef struct wkhtmltoimage_converter wkhtmltoimage_converter; - -typedef void (*wkhtmltoimage_str_callback)(wkhtmltoimage_converter * converter, const char * str); -typedef void (*wkhtmltoimage_int_callback)(wkhtmltoimage_converter * converter, const int val); -typedef void (*wkhtmltoimage_void_callback)(wkhtmltoimage_converter * converter); - -CAPI(int) wkhtmltoimage_init(int use_graphics); -CAPI(int) wkhtmltoimage_deinit(); -CAPI(int) wkhtmltoimage_extended_qt(); -CAPI(const char *)wkhtmltoimage_version(); - -CAPI(wkhtmltoimage_global_settings *) wkhtmltoimage_create_global_settings(); - -CAPI(int) wkhtmltoimage_set_global_setting(wkhtmltoimage_global_settings * settings, const char * name, const char * value); -CAPI(int) wkhtmltoimage_get_global_setting(wkhtmltoimage_global_settings * settings, const char * name, char * value, int vs); - -CAPI(wkhtmltoimage_converter *) wkhtmltoimage_create_converter(wkhtmltoimage_global_settings * settings, const char * data); -CAPI(void) wkhtmltoimage_destroy_converter(wkhtmltoimage_converter * converter); - -CAPI(void) wkhtmltoimage_set_warning_callback(wkhtmltoimage_converter * converter, wkhtmltoimage_str_callback cb); -CAPI(void) wkhtmltoimage_set_error_callback(wkhtmltoimage_converter * converter, wkhtmltoimage_str_callback cb); -CAPI(void) wkhtmltoimage_set_phase_changed_callback(wkhtmltoimage_converter * converter, wkhtmltoimage_void_callback cb); -CAPI(void) wkhtmltoimage_set_progress_changed_callback(wkhtmltoimage_converter * converter, wkhtmltoimage_int_callback cb); -CAPI(void) wkhtmltoimage_set_finished_callback(wkhtmltoimage_converter * converter, wkhtmltoimage_int_callback cb); -CAPI(int) wkhtmltoimage_convert(wkhtmltoimage_converter * converter); -/* CAPI(void) wkhtmltoimage_begin_conversion(wkhtmltoimage_converter * converter); */ -/* CAPI(void) wkhtmltoimage_cancel(wkhtmltoimage_converter * converter); */ - -CAPI(int) wkhtmltoimage_current_phase(wkhtmltoimage_converter * converter); -CAPI(int) wkhtmltoimage_phase_count(wkhtmltoimage_converter * converter); -CAPI(const char *) wkhtmltoimage_phase_description(wkhtmltoimage_converter * converter, int phase); -CAPI(const char *) wkhtmltoimage_progress_string(wkhtmltoimage_converter * converter); -CAPI(int) wkhtmltoimage_http_error_code(wkhtmltoimage_converter * converter); -CAPI(long) wkhtmltoimage_get_output(wkhtmltoimage_converter * converter, const unsigned char **); - -#ifdef BUILDING_WKHTMLTOX -#include "dllend.inc" -#else -#include -#endif - -#endif /*__IMAGE_H__*/ diff --git a/dsPay/wkhtmltox/include/wkhtmltox/pdf.h b/dsPay/wkhtmltox/include/wkhtmltox/pdf.h deleted file mode 100644 index 0ed2ad4c..00000000 --- a/dsPay/wkhtmltox/include/wkhtmltox/pdf.h +++ /dev/null @@ -1,88 +0,0 @@ -/* -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*- - * vi:set ts=4 sts=4 sw=4 noet : - * - * Copyright 2010-2020 wkhtmltopdf authors - * - * This file is part of wkhtmltopdf. - * - * wkhtmltopdf is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * wkhtmltopdf is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with wkhtmltopdf. If not, see . - */ - -#ifndef __PDF_H__ -#define __PDF_H__ - -#ifdef BUILDING_WKHTMLTOX -#include "dllbegin.inc" -#else -#include -#endif - -struct wkhtmltopdf_global_settings; -typedef struct wkhtmltopdf_global_settings wkhtmltopdf_global_settings; - -struct wkhtmltopdf_object_settings; -typedef struct wkhtmltopdf_object_settings wkhtmltopdf_object_settings; - -struct wkhtmltopdf_converter; -typedef struct wkhtmltopdf_converter wkhtmltopdf_converter; - -typedef void (*wkhtmltopdf_str_callback)(wkhtmltopdf_converter * converter, const char * str); -typedef void (*wkhtmltopdf_int_callback)(wkhtmltopdf_converter * converter, const int val); -typedef void (*wkhtmltopdf_void_callback)(wkhtmltopdf_converter * converter); - -CAPI(int) wkhtmltopdf_init(int use_graphics); -CAPI(int) wkhtmltopdf_deinit(); -CAPI(int) wkhtmltopdf_extended_qt(); -CAPI(const char *) wkhtmltopdf_version(); - -CAPI(wkhtmltopdf_global_settings *) wkhtmltopdf_create_global_settings(); -CAPI(void) wkhtmltopdf_destroy_global_settings(wkhtmltopdf_global_settings *); - -CAPI(wkhtmltopdf_object_settings *) wkhtmltopdf_create_object_settings(); -CAPI(void) wkhtmltopdf_destroy_object_settings(wkhtmltopdf_object_settings *); - -CAPI(int) wkhtmltopdf_set_global_setting(wkhtmltopdf_global_settings * settings, const char * name, const char * value); -CAPI(int) wkhtmltopdf_get_global_setting(wkhtmltopdf_global_settings * settings, const char * name, char * value, int vs); -CAPI(int) wkhtmltopdf_set_object_setting(wkhtmltopdf_object_settings * settings, const char * name, const char * value); -CAPI(int) wkhtmltopdf_get_object_setting(wkhtmltopdf_object_settings * settings, const char * name, char * value, int vs); - - -CAPI(wkhtmltopdf_converter *) wkhtmltopdf_create_converter(wkhtmltopdf_global_settings * settings); -CAPI(void) wkhtmltopdf_destroy_converter(wkhtmltopdf_converter * converter); - -CAPI(void) wkhtmltopdf_set_warning_callback(wkhtmltopdf_converter * converter, wkhtmltopdf_str_callback cb); -CAPI(void) wkhtmltopdf_set_error_callback(wkhtmltopdf_converter * converter, wkhtmltopdf_str_callback cb); -CAPI(void) wkhtmltopdf_set_phase_changed_callback(wkhtmltopdf_converter * converter, wkhtmltopdf_void_callback cb); -CAPI(void) wkhtmltopdf_set_progress_changed_callback(wkhtmltopdf_converter * converter, wkhtmltopdf_int_callback cb); -CAPI(void) wkhtmltopdf_set_finished_callback(wkhtmltopdf_converter * converter, wkhtmltopdf_int_callback cb); -/* CAPI(void) wkhtmltopdf_begin_conversion(wkhtmltopdf_converter * converter); */ -/* CAPI(void) wkhtmltopdf_cancel(wkhtmltopdf_converter * converter); */ -CAPI(int) wkhtmltopdf_convert(wkhtmltopdf_converter * converter); -CAPI(void) wkhtmltopdf_add_object( - wkhtmltopdf_converter * converter, wkhtmltopdf_object_settings * setting, const char * data); - -CAPI(int) wkhtmltopdf_current_phase(wkhtmltopdf_converter * converter); -CAPI(int) wkhtmltopdf_phase_count(wkhtmltopdf_converter * converter); -CAPI(const char *) wkhtmltopdf_phase_description(wkhtmltopdf_converter * converter, int phase); -CAPI(const char *) wkhtmltopdf_progress_string(wkhtmltopdf_converter * converter); -CAPI(int) wkhtmltopdf_http_error_code(wkhtmltopdf_converter * converter); -CAPI(long) wkhtmltopdf_get_output(wkhtmltopdf_converter * converter, const unsigned char **); - -#ifdef BUILDING_WKHTMLTOX -#include "dllend.inc" -#else -#include -#endif - -#endif /*__PDF_H__*/ diff --git a/dsPay/文档.md b/dsPay/文档.md index 54310efc..4947f07d 100644 --- a/dsPay/文档.md +++ b/dsPay/文档.md @@ -4,7 +4,3 @@ https://github.com/Javen205/IJPay 发票 HTML( invoice ) https://github.com/LeeXhuan/Invoice - -HTML转PDF -https://cloud.tencent.com/developer/article/1997197 -https://wkhtmltopdf.org/ \ No newline at end of file