sapui5将base64字符串编码为pdf

时间:2018-03-07 08:34:26

标签: javascript pdf base64 sapui5

我有一个base64字符串,我想将其转换为PDF文件。我正在使用SAP UI5框架。我已经用atob()尝试了它,但pdf没有打开。 有什么建议怎么做? BR, ajsnub

这是我的编码:

var base64 = "JVBERi0xLjUNCi...." //shortend                    
var doc = new jsPDF();
                    var SampleData = 'data:application/pdf;base64,' + base64;
                    //doc.image(SampleData, 10, 10);
                    doc.save('test_document.pdf');

使用jsPDF更新:

    private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
    FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel file","xlsx", "xls"); 
    final JFileChooser fc = new JFileChooser();
    fc.setFileFilter(filter);
    fc.setDialogTitle("Excel file selector");
    int returnVal = fc.showOpenDialog(mainMenuPanel);
    if (returnVal == JFileChooser.APPROVE_OPTION){
        jDialogaddItem.setVisible(true);
        try{
            ArrayList<Item> itemList2 = new ArrayList<>();
            Item item2;
            String filepath = fc.getSelectedFile().getAbsolutePath();
            XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(filepath));
            XSSFSheet sheet = wb.getSheetAt(0);
            Iterator<Row> rowIterator = sheet.iterator();
            while(rowIterator.hasNext()){
                Row row = rowIterator.next();
                Iterator<Cell> cellIterator = row.cellIterator(); 
                while(cellIterator.hasNext()){
                    Cell cell = cellIterator.next();                
                    item2 = new Item(cell.getStringCellValue(),cell.getStringCellValue(),cell.getStringCellValue(),cell.getStringCellValue(), (int)cell.getNumericCellValue());
                    itemList2.add(item2);    
                }
            }
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
    }
}         

2 个答案:

答案 0 :(得分:0)

使用&#39;下载&#39; <a>标记的属性 Here the docu

  

使用blob:URL时,[下载]的真正好处是   和文件系统:URL URL。它将为用户提供下载内容的方法   在您的应用中创建/修改。

所以在href中设置base64 blob

同样回顾一下:How to download PDF automatically using js?

使用sap.ui.core.HTML控件打包<a>代码

答案 1 :(得分:0)

检查一下......

$(document).ready(function () {

    var base64 = "VGhpcyBpcyB0ZXh0IGZvciBwZGYgZG9j" //shortend
    var sDecodedFile = window.atob(base64);
    var doc = new jsPDF();
    doc.text(20, 20, sDecodedFile);
    doc.addPage();
    doc.text(20, 20, 'Do you like that?');

// Output as Data URI
    var datat = doc.output('datauri');
    var fileName = 'test.pdf';
    var a = document.createElement("a");
    document.body.appendChild(a);
    a.style = "display: none";
    a.href = datat;
    a.download = fileName;
    a.click();
    window.URL.revokeObjectURL(datat);

});