保存的base64字符串为PDF在使用JavaScript客户端 [英] Save base64 string as PDF at client side with JavaScript

查看:243
本文介绍了保存的base64字符串为PDF在使用JavaScript客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以在这里我的问题:我有一个PDF文件作为我从服务器获取一个base64字符串。我想利用这个字符串来直接显示PDF浏览器或给它的选择另存为...一个链接,当点击。在这里,code,我使用:

So here my problem: I have a pdf file as a base64 String that i am getting from the server. I would like to use this string to either display the PDF directly to the browser or give it a option of "Save as..." when clicking on a link. Here the code i am using:

<!doctype>
<html>
<head>
   <title>jsPDF</title>
   <script type="text/javascript" src="../libs/base64.js"></script>
   <script type="text/javascript" src="../libs/sprintf.js"></script>
   <script type="text/javascript" src="../jspdf.js"></script>

       <script type="text/javascript">

        function demo1() {
            jsPDF.init();
            jsPDF.addPage();
            jsPDF.text(20, 20, 'Hello world!');
            jsPDF.text(20, 30, 'This is client-side Javascript, pumping out a PDF.');

            // Making Data URI
            var out = jsPDF.output();
            var url = 'data:application/pdf;base64,' + Base64.encode(out);

            document.location.href = url;
         }
    </script>
</head>
<body>

<a href="javascript:demo1()">Run Code</a>

</body>
</html>

与Chrome和Safari其工作罚款。 Firefox没有认识到PDF但作为FF需要扩展为present但数据URI现在没有在这种情况下不显示它。究其原因我在这里坚持,如果Chrome和Safari得到它TI的工作,那么就必须有对FF和IE浏览器的解决方案

Its working fine with Chrome and Safari. Firefox does recognize the pdf but does not display it as FF requires extensions to be present but the data-uri has none in this case. The reason im insisting here, if chrome and safari get it ti work, then there has to be a solution for FF and IE

我知道有一些相关的问题,这一点,但没有真正确切的之一,现在也有点旧的。我认识的一个解决方法是在服务器端已经生成的PDF格式,但我想在客户端生成它。

I know there are a few relevant questions to this but not really the exact one and now also a bit old ones. I know a workaround would be to have the pdf generated at server side but i would like to generate it at client side.

所以,请乡亲们聪明,有没有可能通过一些黑客或额外JS下载插件吗?

So please intelligent folks, is it possible through some hacks or additional JS download plugins?

推荐答案

您应该可以使用下载文件

You should be able to download the file using

window.open("data:application/pdf;base64," + Base64.encode(out));

这篇关于保存的base64字符串为PDF在使用JavaScript客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆