jsPDF を使用して Web アプリで PDF ドキュメントを作成し、そのドキュメントを Perl に送信し、Perl にメールで送信すると、正常に動作します。ただし、PDF ドキュメントに画像を追加すると、Adobe Reader がファイルが破損していると表示され、機能しなくなります。アプリは巨大なので、同じように動作する同様の関連コードを含むスタブを次に示します。
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>
js:
$(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);
}
});
});
});
パール:
#!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 の呼び出しと出力の作成を次のように置き換えると...
doc.output('dataurlnewwindow',{});
...その後、新しいブラウザー タブに正しく表示されるので、画像が正しく挿入されていることがわかります。検索で見つけたものから、エンコードの問題のようですが、問題の解決策はまだ見つかりません. サーバー上の Perl に正常に送信された画像付きの PDF ドキュメントを取得して、破損しないようにするにはどうすればよいですか?