使用java将base64转换为pdf

时间:2017-07-03 12:38:30

标签: java pdf base64

我已经使用JSPdf将Pdf转换为base64。我需要再次将base64转换为pdf。我需要使用java实现这一点。我有谷歌没有明确的例子。我的jquery是转换pdf并获得base 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

1 个答案:

答案 0 :(得分:0)

我用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;
}