懒懒羊 2015-01-21 09:38 采纳率: 0%
浏览 5067

wkhtmltopdf 在javaWeb中乱码的问题

我是在web项目中使用wkhtmltopdf,有时会遇到表单,所以必须要用到--post的参数
但是这个参数中加入中文,就会出现乱码。只有在post中会出现乱码,如果只是单纯的html就不会出现这个情况。

 ![图片说明](https://img-ask.csdn.net/upload/201501/21/1421832966_615937.png)

 ![图片说明](https://img-ask.csdn.net/upload/201501/21/1421833038_158586.png)


 wkhtmltopdf:

F:\wkhtmltopdf\bin\wkhtmltopdf.exe --post order.orderId 3 --post order.deliveryTime 3 --post order.productName 杨程巍 --post order.status 3 http://localhost:8080/PDFTest/create_PDF.action "F:\Tomcat 7\webapps\PDFTest\pdfFile\order.pdf"

  • 写回答

1条回答 默认 最新

  • 懒懒羊 2015-01-21 09:41
    关注
     public String createDataPDF(){
    
    
            List<HashMap<String, String>> options=new ArrayList<HashMap<String,String>>();
            //post参数
            HashMap<String,String> orderId=new HashMap<String, String>();
            orderId.put("optionName", "--post order.orderId");
            orderId.put("optionValue",this.order.getOrderId()+"");
    
    
            HashMap<String,String> deliveryTime=new HashMap<String, String>();
            deliveryTime.put("optionName", "--post order.deliveryTime");
            deliveryTime.put("optionValue",BASE64Util.encode(this.order.getDeliveryTime()+""));
    
    
            HashMap<String,String> productName=new HashMap<String, String>();
            productName.put("optionName", "--post order.productName");
            productName.put("optionValue",BASE64Util.encode(this.order.getProductName()));
    
    
            HashMap<String,String> status=new HashMap<String, String>();
            status.put("optionName", "--post order.status");
            status.put("optionValue",BASE64Util.encode(this.order.getStatus()));
    
            HashMap<String,String> pageSize=new HashMap<String, String>();
            pageSize.put("optionName", "--page-size");
            pageSize.put("optionValue","a4");
    
            options.add(orderId);
            options.add(deliveryTime);
            options.add(productName);
            options.add(status);
            options.add(pageSize);
    
            this.wkhtmltopdf.setFileName("order.pdf");
            this.wkhtmltopdf.setObjectPrintUrl("http://localhost:8080/PDFTest/create_PDF.action");
            this.wkhtmltopdf.setOptions(options);
    
    
            String temp= ServletActionContext.getServletContext().getRealPath("/");
            String pdfRealyPath=temp+"pdfFile\\";
            PDFToll pdfToll=new PDFToll(this.wkhtmltopdf);
            pdfToll.createPDF(pdfRealyPath);
    
            return SUCCESS;
        }
    
     <struts>
        <!-- 设置编码格式 -->
        <constant name="struts.i18n.encoding" value="UTF-8"></constant>
        <!-- 创建 -->
        <package name="pdfTest" extends="struts-default">
            <action name="create_PDF" method="createPDF" class="com.test.action.CreatePDF">
                <result name="success">/objectPage.jsp</result>
            </action>
    
        <!-- 创建一个带参数的pdf -->
            <action name="create_Data_PDF" method="createDataPDF" class="com.test.action.PrintPageWithData">
                <result type="redirectAction">
                    download_PDF?wkhtmltopdf.fileName=${wkhtmltopdf.fileName}
                </result>
            </action>
    
        <!-- 下载 -->
            <action name="download_PDF"  class="com.test.action.PDFPrint">
                <result name="success"  type="stream">
                    <param name="contentType">application/pdf;charset=ISO8859-1</param>
                    <param name="inputName">inputStream</param>
                    <param name="contentDisposition">attachment;filename="${wkhtmltopdf.fileName}"</param>
                    <param name="bufferSize">4096</param>
                </result>
            </action>
        </package>
    </struts>
    
     //跳转到目标页面
            public String createPDF() throws UnsupportedEncodingException{
    
    
                this.order.setProductName(BASE64Util.decode(this.order.getProductName()));
                this.order.setDeliveryTime(BASE64Util.decode(this.order.getDeliveryTime()));
                this.order.setStatus(BASE64Util.decode(this.order.getStatus()));
    
                System.out.println(this.order.getDeliveryTime()+"\n"+BASE64Util.decode(this.order.getDeliveryTime())+"\n"+BASE64Util.getFromBASE64(this.order.getDeliveryTime()));
                return SUCCESS;
            }
    

    ps:我用base64转了下码发现也不行,具体原因也不是很清楚,各位有没有遇到这种情况

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)