繁体   English   中英

使用Java将base64转换为pdf

[英]Convert base64 to pdf using java

我已经使用JSPdf将Pdf转换为base64。 而且我需要再次将base64转换为pdf。 我需要使用Java来实现。 我有谷歌没有明确的例子。 我的jquery是将pdf转换为64,如下所示

$('#export').click(function () {
            $('#officeUse').hide();
            $("#customerdata2clone").html($("#customerdata2").html());
            $('#customerdata2').hide();
            $('#customerdata3').show();
            var imgData2;
            var imgData3;
            setTimeout(function(){html2canvas($("#customerdata2clone"), {
                 onrendered: function(canvas) {         
                     imgData2 = canvas.toDataURL(
                         'image/jpeg');              

                 }
             });}, 1000);
             setTimeout(function(){ html2canvas($("#customerdata3"), {
                 onrendered: function(canvas) {         
                     imgData3 = canvas.toDataURL(
                         'image/jpeg');              

                 }
             });}, 2000);
             setTimeout(function(){ html2canvas($("#customerdata"), {
                 onrendered: function(canvas) {         
                     var imgData = canvas.toDataURL(
                         'image/jpeg');              
                     var doc = new jsPDF("p", "px", "a4");
                     doc.context2d.pageWrapYEnabled = true;
                     var width = doc.internal.pageSize.width;    
                     var height = doc.internal.pageSize.height;
                     var pageHeight= doc.internal.pageSize.height;
                     doc.addImage(imgData, 'JPEG', 5, 5, width-10, height-10);
                     doc.addPage();
                     doc.addImage(imgData2, 'JPEG', 5, 5,width-10, height-10);
                     doc.addPage();
                     doc.addImage(imgData3, 'JPEG', 5, 5,width-10, height-405);
                     alert("base 64:"+doc.output('datauristring'));
                     doc.save($("#tFullName").html()+'_'+strDate+'.pdf');
                     $('#customerdata2').show();
                     $('#officeUse').show();
                     $('#focus').focus();
                     $("#customerdata3").hide();
                     $("#customerdata2clone").html('');
                 }
             }); }, 3000);

        });

返回的基数为64

data:application/pdf;base64,JVBERi0xLjMKMyAwIG9iago8PC9UeXBlIC9QYWdlCi9QYXJlbnQgMSAwIFIKL1Jlc291cmNlcyAyIDAgUgovTWVkaWFCb3ggWzAgMCA1OTUuMjggODQxLjg5XQovQ29udGVudHMgNCAwIFIKPj4KZW5kb2JqCjQgMCBvYmoKPDwvTGVuZ3RoIDUyPj4Kc3RyZWFtCjAuMjcgdwowIEcK...........

如何使用Java转换为PDF

我已经使用Java实现了这一点。 解决方案是我已使用Ajax将字节数组传输到控制器,而我只是使用sun.misc.base64将其转换为base64。

使用jQuery获取base64

 var doc = new jsPDF("p", "px", "a4");
                     doc.context2d.pageWrapYEnabled = true;
                     var width = doc.internal.pageSize.width;    
                     var height = doc.internal.pageSize.height;
                     var pageHeight= doc.internal.pageSize.height;
                     doc.addImage(imgData, 'JPEG', 5, 5, width-10, height-10);
                     doc.addPage();
                     doc.addImage(imgData2, 'JPEG', 5, 5,width-10, height-10);
                     doc.addPage();
                     doc.addImage(imgData3, 'JPEG', 5, 5,width-10, height-405);
                     var fileBytes = doc.output('datauristring');
  //Send the base64 value to controller using Ajax

import sun.misc.BASE64Decoder;
import com.itextpdf.text.pdf.codec.Base64;

@SuppressWarnings("restriction")
@RequestMapping("/exportpdftomail")
@ResponseBody
public String exportPdfToMail(@RequestParam("passengerName") String passengerName,
        @RequestParam("userEmail") String userEmail, @RequestParam("fileId") String fileId,
        @RequestBody byte[] fileBytes, HttpServletResponse response, HttpServletRequest request,
        HttpSession session) throws IOException {

    String isMailSent = "true";
    String rootPath = System.getProperty("catalina.home");
    String jsonStr = new String(fileBytes);
    String body = null;
    String fileBytesString = null;
    String userName = null;

    JSONParser parser = new JSONParser();
    JSONObject jObject = null;

    FileOutputStream fop = null;
    File file = null;
    Mail mail = null;
    Integer id = null;
    try {
        id = (Integer) session.getAttribute("userId");
        userName = "" + session.getAttribute("userName");
        JSONObject object = new JSONObject();
        if (id != null && id != 0) {
            object = userService.getUserForMyProfile(id);
        }

        File dir = new File(rootPath + File.separator + "tmpFiles");
        mail = new Mail();
        if (!dir.exists()) {
            dir.mkdirs();
        }

        jObject = (JSONObject) parser.parse(jsonStr);

        body = "<html><body>Dear " + passengerName
                + ", <br/> <br/> Please find the attached document of your application.."
                + "<br/><br/>Thanks & Regards,<br/>" + userName + "<br/>" + object.get("location")
                + "</body></html>";

        fileBytesString = (String) jObject.get("fileBytes");
        //Splitting the base64 values apart from it's type
        fileBytesString = fileBytesString.substring(28);
        //Decode the bytes using itext 64 decoder
        byte[] decodedBytes = Base64.decode(fileBytesString);

        //Decode the String using sun.misc.bas64decoder
        decodedBytes = new BASE64Decoder().decodeBuffer(fileBytesString);
        file = new File("" + dir + File.separator + fileId);
        fop = new FileOutputStream(file);
        fop.write(decodedBytes);
        isMailSent = mail.sendMailAttachment(userEmail, "Application details", body, "" + file, fileId);

    } catch (Exception e) {
        isMailSent = "false";
        e.printStackTrace();
    } finally {
        fop.flush();
        fop.close();
        file.delete();
    }
    return isMailSent;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM