Echarts export to pdf format

[html] view plain copy
exports Echarts diagrams and saves them in pdf format. <span style="background-color: rgb(255, 255, 255); font-family: "microsoft yahei";">I found that Echarts seems to only support png and jpg export, not pdf export. I thought I could only convert png to pdf in the background. </span> 
First introduce the code of the jsp interface.


[html] view plain copy
var thisChart = echarts.init(document.getElementById('myChart')); 
$('#activeResourcesExportBtn').on('click',function(){   
    var chartExportUrl = 'isms/activeResource/export .do'; 
    var picBase64Info = thisChart.getDataURL();//Get the base64 encoding of the echarts chart in png format.                                                       
    $('#chartForm').find('input[name="base64Info"]').val( picBase64Info);//Assign the encoding to the input box 




 











public class ExportPdfController { 
    private static final Logger LOGGER = LoggerFactory.getLogger(ExportPdfController.class); 
 
    @RequestMapping(value = "export", method = RequestMethod.POST) 
    @ResponseBody 
    public ResponseEntity chartExport(HttpServletResponse response, String base64Info, HttpServletRequest request) throws IOException { 
        String newFileName; 
        newFileName = "统计图" + System.currentTimeMillis() + ".pdf"; 
        String newPngName = newFileName.replaceFirst(".pdf", ".png"); 
        String exportFilePath = "d:/test"; 
        base64Info = base64Info.replaceAll(" ", "+"); 
        BASE64Decoder decoder = new BASE64Decoder(); 
        String[] arr = base64Info.split("base64,"); 
        byte[] buffer; 
        try { 
            buffer = decoder.decodeBuffer(arr[1]); 
        } catch (IOException e) { 
            throw new RuntimeException(); 
        } 
        OutputStream output = null; 
        try { 
            output = new FileOutputStream(new File(exportFilePath + newPngName));//生成png文件 
            output.write(buffer); 
            output.flush(); 
            output.close(); 
        } catch (UnsupportedEncodingException e) { 
            e.printStackTrace(); 
        } 
        Pdf(exportFilePath + newPngName, exportFilePath + newFileName); 
        File f = new File(exportFilePath + newPngName); 
        if (f.exists()) { 
            f.delete(); 
        } 
 
        try { 
            byte[] fileBytes = FileUtils.readFileToByteArray(new File(exportFilePath + newFileName)); 
            HttpHeaders headers = new HttpHeaders(); 
            String fileName = newFileName; 
            if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) { 
                fileName = URLEncoder.encode(fileName, "UTF-8"); 
            } else { 
                fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1"); 
            } 
            headers.setContentDispositionFormData("attachment", fileName); 
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); 
            headers.setPragma("public"); 
            headers.setCacheControl("max-age=0"); 
            return new ResponseEntity<>(fileBytes, headers, HttpStatus.OK); 
        } catch (IOException e) { 
            LOGGER.error("IOException:", e); 
            return new ResponseEntity<>(null, HttpStatus.NO_CONTENT); 
        } 
 
    } 
 
    //通过png文件来生成pdf文件 
    public File Pdf(String imagePath, String mOutputPdfFileName) { 
        Document doc = new Document(PageSize.A4, 20, 20, 20, 20); 
        try { 
            PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(mOutputPdfFileName)); 
            doc.open(); 
            doc.newPage(); 
            Image png1 = Image.getInstance(imagePath); 
            float heigth = png1.getHeight(); 
            float width = png1.getWidth(); 
            int percent = this.getPercent2(heigth, width); 
            png1.setAlignment(Image.MIDDLE); 
            png1.setAlignment(Image.TEXTWRAP); 
            png1.scalePercent(percent + 3); 
            doc.add(png1); 
            doc.close(); 
        } catch (FileNotFoundException e) { 
            e.printStackTrace(); 
        } catch (DocumentException e) { 
            e.printStackTrace(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
 
        File mOutputPdfFile = new File(mOutputPdfFileName); 
        if (!mOutputPdfFile.exists()) { 
            mOutputPdfFile.deleteOnExit(); 
            return null; 
        } 
        return mOutputPdfFile; 
    } 
 
    private int getPercent2(float h, float w) { 
        int p = 0; 
        float p2 = 0.0f; 
        p2 = 530 / w * 100; 
        p = Math.round(p2); 
        return p; 
    } 
 
    //输入流读取到输出流 
    private void copy(BufferedInputStream input, BufferedOutputStream outputString) { 
        byte[] but = new byte[1024]; 
        try { 
            while (input.read() != -1) { 
                int by = input.read(but); 
                outputString.write (but, 0, by); 
                outputString.flush(); 
            } 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
    } 


The code on the surface is the code of the controller layer and the main processing logic in the background. It should be imported into the itext package before using the code, it should be used to generate the pdf file. Most of the code is io stream stuff, so I won't introduce it in detail. hopefully this can provide beneficial help to everyone.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326847276&siteId=291194637