问题描述
我要创建与jsPDF一个web应用程序中的PDF文档,发送该文件给Perl,并具有Perl的电子邮件发送,并能正常工作。然而,当我将图像添加到PDF文档,它不再工作,为Adobe Reader的说,文件已损坏。该应用程序是巨大的,所以这里是用类似相关的code存根的行为以同样的方式:
I'm creating a PDF document in a web app with jsPDF, sending that document to Perl, and having Perl email it, and it works fine. However, when I add an image to the PDF document, it no longer works, as the Adobe Reader says the file is corrupt. The app is huge, so here is a stub with similar pertinent code that acts in the same way:
HTML:
<!DOCTYPE html>
<html>
<head>
<script src="https://<myserver>/js/jquery.js"></script>
<script src="https://<myserver>/js/jspdf.js"></script>
<script src="https://<myserver>/js/jspdf.plugin.addimage.js"></script>
<script src="https://<myserver>/test/pdf.js"></script>
</head>
<body>
<input type="submit" id="go">
</body>
</html>
记者:
$(document).ready(function() {
$('#go').on('click',function() {
//create PDF
var imgData = 'data:image/jpeg;base64,<dataurlencoded image string>';
var doc = new jsPDF('p','pt','a4');
doc.addImage(imgData, 'JPEG', 22, 22, 138, 28);
doc.text(30, 120, 'Lorem Ipsum!');
var perl_pdf = doc.output();
//send PDF to perl and have perl email it
$.ajax({
type: "POST",
url: "https://<myserver>/cgi-bin/pdf.pl",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
dataType: "json",
data: "perl_pdf="+encodeURIComponent(perl_pdf),
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("error: "+ XMLHttpRequest.responseText + ", textStatus: " + textStatus + ", errorThrown: " + errorThrown);
},
success: function(data){
alert("Success: "+data.success);
}
});
});
});
perl的:
perl:
#!d:/perl/bin/perl.exe -w
use strict;
use warnings;
use CGI qw(:all);
use MIME::Lite;
use MIME::Base64;
my $q = CGI->new();
my $pdf_doc = $q->param('perl_pdf');
open (OUTFILE, '>pdf.pdf') or die "Could not open file";
binmode(OUTFILE);
print OUTFILE decode_base64($pdf_doc);
close OUTFILE;
my $from_address = '<from_address>';
my $to_address = '<to_address>';
my $mail_host = '<smtp_server>';
my $subject = 'PDF Test';
my $message_body = "The PDF is attached...\n\n";
my $my_file = 'pdf.pdf';
my $out_file = 'test.pdf';
my $msg = MIME::Lite->new (
From => $from_address,
To => $to_address,
Subject => $subject,
Type => 'multipart/mixed') or die "Cannot create multipart container: $!\n";
$msg->attach (
Type => 'TEXT',
Data => $message_body) or die "Cannot attach text: $!\n";
$msg->attach (
Type => 'application/pdf',
Path => $my_file,
Filename => $out_file,
Disposition => 'attachment') or die "Cannot attach file: $!\n";
MIME::Lite->send('smtp', $mail_host, Timeout=>60);
$msg->send;
my $json = qq{{"success" : "This worked"}};
print $q->header(-type => "application/json", -charset => "utf-8");
print $json;
如果我取代Ajax调用和输出创造与...
If I replace the Ajax call and output creation with...
doc.output('dataurlnewwindow',{});
...然后正确地在新浏览器选项卡显示,所以我知道图像被正确插入。从我在我搜索发现,这似乎是一些编码的问题,但我还没有找到一个解决问题的办法。我怎样才能得到PDF文档,与图像,发送到Perl的服务器上的成功,因此,它是不是腐败?
...then it correctly displays in a new browser tab, so I know the image is being inserted correctly. From what I've found in my searches, it seems to be some encoding issue, but I have not yet found a solution to the problem. How can I get the PDF document, with the image, sent over to Perl on the server successfully, so that it is not corrupt?
推荐答案
我发现这个问题。之前添加图像的PDF,将文件发送到的Perl的工作没有编码,这显然是由于没有(或没有相关)在发送的字符串丢失二进制信息。当然,加入的图像添加非常相关的二进制信息而无法在一个urlen $ C $光盘消息要发送的字符串。编码/解码应该采取这种照顾,但...
I found the problem. Before adding the image to the PDF, sending the file to Perl worked without encoding, apparently because there was no (or no pertinent) binary information lost in the sending of the string. Of course, adding the image added very pertinent binary information to the string which could not be sent in a urlencoded message. Encoding/decoding should have taken care of this, but...
我尝试了许多不同的Base64编码/解码方法,但我的文件仍然结束了损坏。我终于在一个类似的问题迷迷糊糊如果有人提到,发送字符串作为URL的一部分会+符号转换为空格。我取消了对Perl的侧解码,看看有什么恩codeD字符串样子,的确有几个空格整个字符串。与这些转换回
I tried many different Base64 encoding/decoding methods, but my file still ended up corrupt. I finally stumbled on a similar issue where someone mentioned that sending the string as part of the URL will convert + signs to spaces. I removed the decoding on the Perl side to see what the encoded string looked like and there were indeed several spaces throughout the string. Converting these back with
$pdf_doc =~ s/ /+/g;
之前有Perl的写入文件解决了该问题。该文件现在能够被向上拉在Adobe在服务器侧
prior to having Perl write to the file fixed the issue. The file is now able to be pulled up in Adobe on the server side.
这篇关于图像中产生的PDF腐化PDF发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!