我从API响应中获取字符串作为字符串:
$scope.image =
"/9j/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAAUCAB4AMgEASIAAhEBAxEBBCIA/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADgQBAAIRAxEEAAA/APn+iiigD5/ooooooooAKKKKKKKKACiiiiiiigAooooooooAKKKKKKKKACiiiiiiigAooooooooAKKKKKKKKACiiiiiiigAooooooooAKKKKKKKKACiiiiiiigAooooooooAKKKKKKKKACiiiiiiigAooooooooAKKKKKKKKACiiiiiiigAooooooooAKKKKKKKKACiiiiiiigAooooooooAKKKKKKKKACiiiiiiigAooooooooAKKKKKKKKACiiiiiiigAooooooooAK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z16/6z/a6+9Z9FOEkgZmDtlwQxz1z1zTaKKALiatqUdxd3CahdrPeo6XUizMGnVzlw5zlgx5IOc96p0UUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRX/2Q=="
现在我想将其转换回图像,然后以PDF格式下载。
以下不起作用:
window.open('data:application/pdf;base64,' + $scope.image);
我也尝试过使用pdfMake,但这也不起作用:
var dlnk = document.getElementById('dwnldLnk');
html2canvas(document.getElementById('dwnldLnk'), {
onrendered: function (canvas) {
var data = canvas.toDataURL();
var docDefinition = {
content: [{
image: data,
width: 500
}]
};
pdfMake.createPdf(docDefinition).download("Roster.pdf");
}
});
HTML客户端的div为:
<div id="dwnldLnk" style="display:none;">
{{image}}
</div>
在服务器端,这是存储字节的代码:
String html = "<h1>Hello, world.</h1>";
JLabel label = new JLabel(html);
label.setSize(200, 120);
BufferedImage image = new BufferedImage(
label.getWidth(), label.getHeight(),
BufferedImage.TYPE_INT_ARGB);
{
// paint the html to an image
Graphics g = image.getGraphics();
g.setColor(Color.BLACK);
label.paint(g);
g.dispose();
}
// get the byte array of the image (as jpeg)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(image, "jpg", baos);
} catch (IOException e) {
e.printStackTrace();
}
// these bytes are stored in bytea column in postgres database
byte[] bytes = baos.toByteArray();
答案 0 :(得分:0)
虽然你的图像在pdf中没有正确显示我用其他图像base64字符串检查并且它们运行良好。因此,您还需要检查如何将图像转换为base64字符串。对我有用的是:
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/jspdf@latest/dist/jspdf.min.js"></script>
</head>
<body>
<button id="download">download</button>
<script>
var image = new Image();
image.src = 'data:image/jpeg;base64,/9j/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAAUCAB4AMgEASIAAhEBAxEBBCIA/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADgQBAAIRAxEEAAA/APn+iiigD5/ooooooooAKKKKKKKKACiiiiiiigAooooooooAKKKKKKKKACiiiiiiigAooooooooAKKKKKKKKACiiiiiiigAooooooooAKKKKKKKKACiiiiiiigAooooooooAKKKKKKKKACiiiiiiigAooooooooAKKKKKKKKACiiiiiiigAooooooooAKKKKKKKKACiiiiiiigAooooooooAKKKKKKKKACiiiiiiigAooooooooAKKKKKKKKACiiiiiiigAooooooooAKKKKKKKKACiiiiiiigAooooooooAKKKKKKKKACiiiiiiigAooooooooAKKKKKKKKACiiiiiiigAooooooooAKKKKKKKKACiiiiityKG1WNGtVSQeYm6Rm+cfP6Y4/SobwW4VrlbcBkuirAsTvxyfzoAKK9o0DS/DdteaJN4VgsNSh/tOx+1ahdXZ+2wf6TH0gIUIC2Fyu/gnmqPjjTNES11TxSdK3XFt4zuLG4iNw+26iAMjBv7uSCPlAwD3IzWTRW4wSe7jYwxFDaBtrttCjPY47VE0dolvfLBIxiwmSBuwdx4HTI6UAeS0V7f4gji1TxP4bkk0XRZrObwfazXMN3P9kgtkaQkujg5QgkAYycMeDVazsvCdv4J+IUGlanePpJj0wzTRxCdo5PPfKx7vK8xc7fmO3gnrjnIord+zQTBHkRpBHbR7RyM5z1xk9qlt0jjkVYVKxi44BBB/1XvzQB4zRXv2t+HNC1KysdTvXjuIdL8JaWbaO9EsaSLJJIu+RYNzjAA4UkAsMnHNdB4H0zT9GurSw0o3P2GLxdN5IuoXikUHS3bayuAwwSRyOcZ71ztFbP8AZ9v/AGdv8p9/keZ5u7jOM4x/9aqNjbh9QjgnjIBzuVsg9CaAPmGivWP+EH0D/hFdv2ZvP/4Rf+3P7V81/wDX78fZ9udmP4em7PeuT8H+Ho5viLp2geI7CeFZ5fJmt51eJ1LodhI4YclWHrx2NVKK3ptLto7Z2UBmjibccnlsAg9ePp70S6ZaooAhcbXQby33wSAe/v7UAcnRXteq/DTQNL8MX980Svc6NpckGogTPj7eyQtGw56DzWHplBkdall+HXhhdVm04WTLHpuoaRCt2Z5P+JmlyyiUHnAwCSNmMAVg0VtpHbs13DFEVjEkSMCTyd/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M/KR6jFZ9FPE0oLESOCxy2GPJ65NPN1cEgm4lJByPnPFQ0UAXDq+pMl6h1G7K3ziS7BmbFwwbcGk5+chiTk55Oauf8Jb4kEiSf8ACQ6tvSTzUb7bJlX2bNw54Owlc+hx0rHooqQzzMcmWQndu5Y9fX61HRQAVof27rAAH9q32BdfbR/pD/8AHx/z16/6z/a6+9Z9FOEkgZmDtlwQxz1z1zTaKKALiatqUdxd3CahdrPeo6XUizMGnVzlw5zlgx5IOc96p0UUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRRRRRQAUUUUUUUUAFFFFFFFFABRRRX/2Q==';
document.body.appendChild(image);
download.addEventListener("click", function() {
var pdf = new jsPDF();
pdf.addImage(image, 'JPG', 0, 0);
pdf.save("download.pdf");
}, false);
</script>
</body>
</html>
答案 1 :(得分:0)
我在angularjs中提出了这个代码。 html aaaa,3,5,1,3
bb,2,4,,,,,,,,6
cc,,,,,,5
dd,5
和<h1>
看起来像是java中生成的图像的一部分。以下代码已成功下载了带有图像的pdf。
</h1>