简体   繁体   English

如何根据HTML中的内容智能地插入分页符以生成PDF

[英]How to insert a page break intelligently based on content in HTML for generating PDF

I have a continuous HTML page that can go to any height based on user input. 我有一个连续的HTML页面,可以根据用户输入转到任何高度。 When the user is done with inserting content then I generate PDF of the HTML document. 当用户完成插入内容后,我生成HTML文档的PDF。 For HTML to PDF conversation I am using wkhtmltopdf. 对于HTML到PDF对话,我使用的是wkhtmltopdf。

The whole html is composed of small sections. 整个html由小部分组成。 I want that when A new section is about to start and the control is nearly at the end of the page then that section should begin from next page rather some part of it on page 1 and remaining on page 2. 我希望当一个新的部分即将开始并且控件几乎在页面的末尾时,该部分应该从下一页开始,而不是在第1页的某些部分开始,并保留在第2页。

Here is my relevant code: 这是我的相关代码:

HML: HML:

<div id="page-content">
  <div class="section-item></div><!---This is of variable height based on content-->
  <div class="section-item></div><!---This is of variable height based on content-->
  <div class="section-item></div><!---This is of variable height based on content-->
  <div class="section-item></div><!---This is of variable height based on content-->

</div>

JavaScript: JavaScript的:

$(window).load(function() {

var pageSize = 1300;
var currrentPageSpace = pageSize;
$(".section-item").each(function() {

    var sectionPosition = $(this).position().top;
    var sectionHeight = $(this).outerHeight(true); //Height, including the padding and margin

    var availabePageSpace = currrentPageSpace - sectionPosition;
    if (sectionHeight < availablePageSpace) {
        $(this).css("margin-top", availabePageSpace+"px"); //Give this section margin top equal to amount of remaining page space so that this section is shown on next page
        currentPageSpace += pageSize; //Now we are on new page and its available space is adjusted
    }
});
});

But this code has random behavior based on page content. 但是此代码具有基于页面内容的随机行为。 For some content it works perfectly. 对于某些内容,它完美地运作。 Sometimes it does not add margin at all. 有时它根本不会增加保证金。 Sometimes the section-item goes to next page and has lot of margin added the top. 有时,section-item会转到下一页,并且顶部会添加很多边距。

Is there alternative way of achieving page breaks for continuous HTML while generating PDF? 是否有其他方法可以在生成PDF时实现连续HTML的分页符? Any help or input will be highly appreciated. 任何帮助或输入将受到高度赞赏。

I calculate if the square "quadrado" has height to fit in the html page with 100px height without yes it adds a page break in the last one and sums the current height + base height 100px ex. 我计算方形“quadrado”的高度是否适合高度为100px的html页面而不是它在最后一个中添加分页符并将当前高度+基本高度加起来为100px ex。 Current_time_2000px + base_space 1000px = 300px Current_time_2000px + base_space 1000px = 300px

My idea is to do something like google docs or Word in the page break, but for now it comes out even 1.0 version :P. 我的想法是在分页符中做google docs或Word之类的东西,但是现在它甚至出现了1.0版本:P。

I hope I have helped, :) 我希望我有所帮助,:)

 var count_add_class = 1, tamanho_do_elemento = 1, count_soma = 0, i = 1; $(".altura_bloco").each(function() { var a = count_add_class++; // pega a posição do elemento, e adcinando uma classe 1,2,3,4,5,6,7... $(this).addClass("element_height_" + a); tamanho_do_elemento = $(this).outerHeight(); count_soma += tamanho_do_elemento; // console.log(tamanho_do_elemento); console.log(tamanho_do_elemento); if (count_soma > 937) { $(this).addClass("page-break"); count_soma = tamanho_do_elemento; i++; // console.log(tamanho_do_elemento); } }); 
 .altura_bloco { width: 400px; height: 400px; background: #000; margin: 10px; } @media print { .page-break { display: block; page-break-before: always; } } 
 <div class="altura_bloco"><br></div> <div class="altura_bloco"><br></div> <div class="altura_bloco"><br></div> <div class="altura_bloco"><br></div> <div class="altura_bloco"><br></div> <div class="altura_bloco"><br></div> <div class="altura_bloco"><br></div> <div class="altura_bloco"><br></div> <div class="altura_bloco"><br></div> <div class="altura_bloco"><br></div> <div class="altura_bloco"><br></div> <div class="altura_bloco"><br></div> <div class="altura_bloco"><br></div> <div class="altura_bloco"><br></div> <div class="altura_bloco"><br></div> <div class="altura_bloco"><br></div> <div class="altura_bloco" style="height: 500px"><br></div> <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM