image in generated pdf corrupts pdf sent to server

不羁岁月 提交于 2019-12-06 00:08:12
Anil Upadhyay

This issue is resolved in latest version of JSPDF. If you are sending PDF with images to server as form-data using xmlhttprequest.send or as part of ajax call.

please use jsPDF.output('blob') instead of jsPDF.output().

This will not corrupt the pdf when send to server.

jm666

OK, This probably will not help you, because youre using Windows, only demostrating:

My test perl script called as app.pl:

#!/usr/bin/env perl
use strict;
use warnings;
use CGI qw(:all);

my $q = CGI->new();
my $pdf_doc = $q->param('perl_pdf');

open (my $fp, '>', 'pdf.pdf') or die "Could not open file";
print $fp $pdf_doc;
close $fp;
print "OK\n";

Have a pdf file called as x.pdf.

$ ls -l x.pdf
-rw-r--r--@ 1 jm  staff  100838 18 mar 19:20 x.pdf

Running an simple test web-server on port 3000

plackup --port 3000 -MPlack::App::WrapCGI -e 'Plack::App::WrapCGI->new( script => "./app.pl", execute => 1)->to_app'

Says:

HTTP::Server::PSGI: Accepting connections at http://0:3000/

From another terminal sending a base64 encoded file via the curl command

$ curl -i -F perl_pdf="$(base64 < x.pdf)" 0:3000/

response:

HTTP/1.0 200 OK
Date: Tue, 18 Mar 2014 20:15:40 GMT
Server: HTTP::Server::PSGI
Content-Length: 3

OK

the server says:

127.0.0.1 - - [18/Mar/2014:21:06:00 +0100] "POST / HTTP/1.1" 200 3 "-" "curl/7.35.0"

and saved the file pdf.pdf The file has base64 encoded content.

$ ls -la pdf.pdf
-rw-r--r--  1 jm  staff  134452 18 mar 21:06 pdf.pdf

decode it

$ base64 -D < pdf.pdf >decoded.pdf

and compare with original

$ cmp decoded.pdf x.pdf
$ ls -la decoded.pdf 
-rw-r--r--  1 jm  staff  100838 18 mar 21:18 decoded.pdf

no difference - sending the pdf - successsful.

Unfortunately, can't help you more, because:

  • youre using Windows
  • and me don't know Javascript...

Consider to check:

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...

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;

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!