-3

我正在尝试将 HTML 转换为 PDF,所以我使用了 TCPDF 它的 PHP 插件。那是它的官方网站 TCPPDF

它运行良好,但工具有限,字体有限

而且它无法将图像转换为 PDF,所以我如何将带有图像的完整 HTML 页面转换为 PDF 文件,不管它是 JS 还是 PHP 插件,我只想要积极的解决方案,将带有 Css 的整个 HTML 页面转换为 PDF 文件,也许我会保存或打印

4

2 回答 2

0
    $('#showPdf').click(function() {
      var pdf = new jsPDF();  
      pdf.addHTML($("#divContent"), function() {
    	var blob = pdf.output("blob");
        window.open(URL.createObjectURL(blob));
      });
    });
$('#downloadPdf').click(function() {
  var pdf = new jsPDF();  
  pdf.addHTML($("#divContent"), function() {
	pdf.save('pageContent.pdf');
  });
});
   
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.debug.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <div id="divContent" style="background-color: white; padding:20px 25px">
      <h3>SEA MINK</h3>
      <p>The sea mink (Neovison macrodon) was a mammal from the eastern coast
      of North America, in the family of weasels and otters in the order Carnivora.
      The largest of the minks, it was hunted to extinction by fur traders before 1903,
      when it was first given a species description. Some biologists classify it as a subspecies of the
      American mink. Estimates of its size are speculative, based largely on skull fragments recovered 
      from Native American shell middens, and on tooth remains. Some information on its appearance and 
      habits was provided by fur traders and Native Americans. It may have been similar in behavior to
      the American mink: it probably maintained home ranges, was polygynandrous, and had a similar diet, 
      supplemented by saltwater prey. Sea minks were commonly trapped along the coast of the Bay of
      Fundy in the Gulf of Maine. Remains have been found along the New England coast, and there were r
      egular reports of unusually large mink furs, probably sea mink, being collected from Nova Scotia</p>
      
      <img  src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBwgHBgkIBwgKCgkLDRYPDQwMDRsUFRAWIB0iIiAdHx8kKDQsJCYxJx8fLT0tMTU3Ojo6Iys/RD84QzQ5OjcBCgoKDQwNGg8PGjclHyU3Nzc3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msuzsa13U4KSz9l5ls5qKyzVp+wVtyuLL3qGMRysB9EogdptK08STwMhHE8uA559E2bs2nY0uGOrfN/vYYNSo5suwAAAA3LZEZlAEAQBAEBEudto7rSOpbhTR1EDiCWSDOCOBHQjkRvCo0msMHBfYLpbwfquvbVwAfZpriTtDoBMMnH6muO/itBebu2td8VPqPw1X9ezRNGvJc9TBdcozszWaqyOLoZIntPl9oH+AtDV3YvIvqOMvXH3ROriPaYM1aR93Zrg89MRt/7PCshu1fSeuF6+2R0iBg2/UNYdlnodsi3fePJqJSOY2RhrT3y7yW1td16cdbiefBafX/ABEcrh/xJ9m0rbrXUOrCJKu4PbsyVtU7blcOgPBrfZaAOy6ehQp0IfDpRwjHbbeWd0DHBSlAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCAIAgCA/9k="/>
      </div>
      <button id="showPdf">Generate PDF</button>
<button id="downloadPdf">Download PDF</button>
Try this code in your browser for better result.
于 2017-11-22T13:41:39.103 回答
0
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js"></script>
<script type="text/javascript" src="https://html2canvas.hertzen.com/dist/html2canvas.js"></script>


function CreatePDFfromHTML(e) {
    var HTML_Width = $("#imageDIV").width();
    var HTML_Height = $("#imageDIV").height();
    // alert(HTML_Width); // HTML_Height=3800; // HTML_Width=1349;
    var top_left_margin = 15;
    var PDF_Width = HTML_Width + (top_left_margin * 2);
    var PDF_Height = (PDF_Width * 1.5) + (top_left_margin * 5);
    var canvas_image_width = HTML_Width;
    var canvas_image_height = HTML_Height;
    var totalPDFPages = Math.ceil(HTML_Height / PDF_Height) - 1;
    html2canvas($("#imageDIV")[0]).then(function (canvas) {
        var imgData = canvas.toDataURL("image/jpeg", 1.0);
        var pdf = new jsPDF('p', 'pt', [PDF_Width, PDF_Height]);
        pdf.addImage(imgData, 'JPG', top_left_margin, top_left_margin, canvas_image_width, canvas_image_height);
        for (var i = 1; i <= totalPDFPages; i++) {
            pdf.addPage(PDF_Width, PDF_Height);
            pdf.addImage(imgData, 'JPG', top_left_margin, -(PDF_Height * i) + (top_left_margin * 15), canvas_image_width, canvas_image_height);
        }
        pdf.save("calendar.pdf"); 
于 2020-06-13T13:50:45.767 回答