简体   繁体   English

另存为PDF:建议使用服务器解决方案从客户端接收原始数据并将PDF发送回客户端吗?

[英]save as PDF: recommend a server solution to receive raw data from client and send back PDF to client?

My project requires me to add a "SaveAs PDF" feature. 我的项目要求我添加“另存为PDF”功能。 I was looking for a pure JavaScript solution which does this just in client end, however, was told that it's not implementable, at least for now. 我一直在寻找一个纯粹的JavaScript解决方案,该解决方案仅在客户端执行此操作,但是,有人告知它至少在目前还无法实现。

jsPDF currently is still a limited version, not support graph and others. jsPDF当前仍然是受限版本,不支持图表等。 So now I am looking for a stable open-srouce free solution to set up a server web service, that receive data from client-end and send back the produced PDF file or a link for user to save to their disk. 因此,现在我正在寻找一种稳定,开放的免费解决方案来设置服务器Web服务,该服务可以从客户端接收数据并将所产生的PDF文件或链接发送回去,以供用户保存到其磁盘上。

The data from client is determined by client user, which means, not the whole page. 来自客户端的数据由客户端用户确定,也就是说,不是整个页面。 User can choose to save a map, a table, or all of them into PDF file. 用户可以选择将地图,表格或全部保存为PDF文件。

Any recommendations? 有什么建议?

PS: in Windows environment PS:在Windows环境中

You might check out the ReportLab Toolkit - it includes an Open Source Python library for creating PDFs. 您可能会签ReportLab工具箱 -它包括一个用于创建PDF的开源Python库。 (You didn't specify what server-side language you wanted, but Python is pretty widely supported.) (您没有指定所需的服务器端语言,但是Python得到了广泛的支持。)

If you need something that can be coded in Javascript, one option might be PhantomJS . 如果您需要可以用Javascript编码的内容,则可以选择PhantomJS This tool allows you to run a headless Webkit browser from the command line, and among other things it can render and save webpages as PDFs. 使用此工具,您可以从命令行运行无头Webkit浏览器,并且可以将网页呈现和保存为PDF。 Slippy uses this approach, so you might be able to get example code from that project. Slippy使用此方法,因此您可能能够从该项目中获取示例代码。 Scripting the PDF creation would probably be much faster in PhantomJS than in Python, but it's likely to be much slower (it has to fire up a Webkit instance) and server installation might be complicated. 在PhantomJS中编写PDF脚本的脚本可能比在Python中编写脚本要快得多,但它可能要慢得多(它必须启动Webkit实例),并且服务器安装可能很复杂。

I've create this function in javascript which send on iframe to the server: 我已经在javascript中创建了此函数,该函数在iframe上发送到服务器:

function download(url, datas){
    if(url && datas){ 
        var inputs = '', value,
            iframe = '<iframe name="iframeDownload" id="iframeDownload" width=0 height=0></iframe>';

        $(iframe).appendTo('body');

        datas.forEach(function(data){ 
            name = encodeURI(data.get('name'));
            value = encodeURI(data.get('value'));

            inputs+='<input name="'+name+'" value="'+value+'"/>'; 
        });

        $('<form action="'+url+'" method="post" target="iframeDownload">'+inputs+'</form>').appendTo('body').submit().remove(); // .appendTo and remove() are needed for firefox

        $(iframe).remove();
    };
};

I'm encoding the input name and value to be able to send data. 我正在对输入名称和值进行编码,以便能够发送数据。 On my server, I'm using php, so to decode this, you need: rawurldecode. 在我的服务器上,我使用的是php,因此要对此进行解码,您需要:rawurldecode。 If you define the name of the inputs as "fileName" and "file" you can write this: 如果将输入的名称定义为“ fileName”和“ file”,则可以这样编写:

$fileName = rawurldecode($_POST['fileName']);
$file = rawurldecode($_POST['file']);

After than, to force the download, you need to send the corrects header. 之后,要强制下载,您需要发送正确的标头。 I'm using this function: 我正在使用这个功能:

function download($filename, $file) {
    header('Content-disposition: attachment; filename="'.$filename.'"');
    header('Content-Type: application/force-download');
    header('Content-Length: '. filesize($file));
    readfile($file);
}

If you don't need to send the file from javascript because it's created on the server side, just add the path of your file to the download function. 如果您不需要从javascript发送文件(因为它是在服务器端创建的),则只需将文件的路径添加到下载功能即可。


If you're using PHP, You can use fpdf to generate the pdf. 如果您使用的是PHP,则可以使用fpdf生成pdf。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从服务器向客户端发送 PDF - Send PDF from Server to Client 在浏览器中编辑PDF并将其保存回服务器,而无需在客户端计算机上下载PDF - Edit PDF in browser and save it back to server without downloading PDF on client machine 使用JavaScript / AJAX将PDF从客户端保存到服务器 - Save PDF from Client to Server using JavaScript/AJAX 将压缩的 PDF 文件内容从客户端发送到后端服务器 - Send compressed PDF file contents from client side to backend server 将PDF保存到服务器而不是客户端计算机上 - Save PDF to server instead of on client machine 将http请求中的数据保存到数组中,以json的形式发送回客户端 - Save data from http request into an array, to send it as json back to the client 需要将数据从NodeJS服务器发送回JQuery客户端 - Need to send data back from a NodeJS server to a JQuery client 从服务器向客户端发送数据 - send data from server to client 在Pdf.js库中,哪个文件调用read_pdf函数从服务器到客户端获取pdf数据? - In Pdf.js Library , which file calls the read_pdf function to fetch the data of pdf from server to client end? 如何从 url 获取远程 PDF 并使用 Express js 发送到客户端? - How to fetch remote PDF from a url and send to the client with Express js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM