読者です 読者をやめる 読者になる 読者になる

クライアントでpdfを生成するjsPDF

memo javascript

jsPDF - HTML5 PDF Generator | Parallax

たまたま見かけたので、ダウンロードして試してみた。
クライアントサイドでpdfを生成できる。

このコードで

var doc = new jsPDF();
var img = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wAARCABJAEkDAREAAhEBAxEB/8QAHgAAAQQDAQEBAAAAAAAAAAAABgMEBQcCCAkBAAr/xABFEAACAQIEAwQECQcNAQAAAAABAgMEEQAFEiEGBzETIkFRCGFxkRQjMjNCUoGxwTRTVWKh0fAVFhcZNkNUcnN0k7Pi8f/EABoBAAIDAQEAAAAAAAAAAAAAAAECAAMEBQb/xAAwEQABAwMDAgQEBgMAAAAAAAABAAIRAyExBBJBUWETInHwBYGhwRQyQtHh8VKRsf/aAAwDAQACEQMRAD8A5kRgyy65GLserOxYnCLc0QiDPauSqybL4XpKeNIIm0SpCqPLud2N+95dBhwSi5kyUM9iOn4YaVVtWcTy05+KkeFvrRuV+7AN8ogQntPxFmlNuta7geEoD/eMIaTDlqsFao3Dk/j41rOk1PTzbdSCp/HFLtO3gkK0aqoMwVc3MWq5Hwcp+Gzwlmue1PH8DIc5aeNhR1YYd8Qofm+zbpqAuoN7kjHNpN1RrEPYQ2/LSO0ReTzNhwuk+pphTJDhxAG6e8yIt2uqYkqMsqiT2qBj+dQqfeMdCHt4XPLqTuf3TKsoqeJdSEkHpoOoft3xYCTlI5rRhR+iP9bDqqyLc84XoOGa0UtXmFTPN2AZZaKAPTyyMQdKOSLroO56htreOFp1PEbuiPVNUZUou2vTHOaurqcxjoZJXmjhVYIUjpo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9mLeVoUvR/Mn+PLDpQl6j6P8ArD7sHhBMl/LZft/HCjKc4UVn3SL/ADYz1k1PlQJ+eb24zhWKR/uV9mCh+lNKj59PaMRHhSfDn9qMl/30H/auLxhUu/L/AL/6v0tYK5S//9k='
doc.setFontSize(40);
doc.text(40, 20, "Brainstorm");
doc.addImage(img, 'JPEG', 13, 5)
doc.setFontSize(16)
doc.text(40, 40, 'hello')

たしかにpdfが作成できた。


ただし日本語は通らない模様。MIT License。