【问题标题】:Is it possible to create a pdf file from base64 string?是否可以从 base64 字符串创建 pdf 文件?
【发布时间】:2016-01-20 02:29:05
【问题描述】:

我有一个 hl7 文件,其中包含一个从编码的 pdf 派生的 base64 字符串。

是否可以从该 base64 重新创建 pdf?

pdf to base64 --> ok
----------------------------
base64 to pdf --> is this possible?

【问题讨论】:

  • 简短的回答是肯定的,这就是 base64 的意义,将二进制数据与文本相互转换

标签: java pdf base64


【解决方案1】:

这是可能的。您可以使用 sun.misc.BASE64Decoder。

例子:

import java.io.*;

import sun.misc.BASE64Decoder;
/**
 * Kax7ux
 *
 */
public class App 
{
    public static void main( String[] args )
    {     
        String encodedBytes = "yourStringBase64"; 
        try {
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] decodedBytes;
            FileOutputStream fop;
            decodedBytes = new BASE64Decoder().decodeBuffer(encodedBytes);
            File file = new File("path/file.pdf");
            fop = new FileOutputStream(file);

            fop.write(decodedBytes);

            fop.flush();
            fop.close();
            System.out.println("Created");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

【讨论】:

    【解决方案2】:

    @Clem 您可以轻松使用 java.util

    中的 Base64
       import java.util.Base64;
    
        public class App 
        {
            public static void main( String[] args )
            {  
                String pdfAsArrayByte = "JVBERi/8KNyAwIG9iago8PAovVHlwZS....";
    
                // Decode the Base64 arrayByte to PDF file
                DataSource source = new ByteArrayDataSource(Base64.getDecoder().decode(pdfAsArrayByte),"application/pdf");
    
                // The ,source, instance is now a true PDF file
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2022-11-22
      • 1970-01-01
      • 2017-06-19
      • 2018-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-25
      相关资源
      最近更新 更多