christopherdro / react-native-html-to-pdf

Convert html strings to PDF documents using React Native
MIT License
434 stars 264 forks source link

IOS: added one empty page in last position #277

Closed birjubhatt closed 1 year ago

birjubhatt commented 2 years ago

when i create pdf in ios that will add one empty page in end of pdf ...and while check android that time its work fine

let options = { html: ${DynamicHTML(res, name, logo, symbol)}, fileName: "Products_Pdf_Images", directory: "Documents", };

let file = await RNHTMLtoPDF.convert(options);
props.callBack([`file://${file.filePath}`]);

};

function DynamicHTML(res, name, logo, symbol) { var html = ` `; console.log("product .length " + product.length); for (let i = 0; i < product.length; i++) { console.log("i :" + i);

  var logoPresent = logo
    ? `<img src= ${logo} class='inline' width="50" height="50"  style= "position:absolute; bottom: 0; left: 45%;margin-bottom: 10px" >`
    : "";
  html =
    html +
    `<div class = "card">
        <img src= ${res}${product[i].catalog_id}/${
      product[i].file_path
    } class = 'center' alt="Image">
          <div style = "margin: 25px;">
            <table style ="font-size: 20px; ">
              <tr>
                <td>Name :</td>
                <td>${product[i].product_name}</td>
              </tr>
              <tr>
                <td>Desc :</td>
                <td>${
                  product[i].product_description
                    ? product[i].product_description
                    : "-"
                }</td>
              </tr>
              <tr>
                <td>Code :</td>
                <td>${
                  product[i].product_code ? product[i].product_code : "-"
                }</td>
              </tr>
              <tr>
                <td>Price :</td>
                <td>${
                  product[i].product_price
                    ? product[i].product_price + symbol
                    : "-"
                }</td>
              </tr>
            </table>
            <div style="position: relative; margin-top: 60px;">
              ${logoPresent}
              <p class = 'inline' style= "position:absolute; bottom: 0; left: 45%; margin-left: ${
                logo ? "55px" : "0px"
              };font-size: 22px;">${name}</p>
              <img src= ${logo} class ='inline' style="position:absolute; bottom: 0; right: 0;margin-right: 10px; margin-bottom: 10px; opacity: 0.5;" width="50" height="50">
            </div>
          </div>
      </div>`;
}
html = html + "</body></html>";

return html; }

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

ramkrishnasamanta8 commented 7 months ago

This is happening at my side , when I am creating pdf with multiple page and putting custom header and footer in each page .

ramkrishnasamanta8 commented 7 months ago

Solved the problem by making changes the line for ( int i = 0 ; i < self.numberOfPages ; i++ ) to for ( int i = 0 ; i < self.numberOfPages - 1 ; i++ ) in the function

Note : You can pass value from Javascript code like

let options = { html: htmlCode, type: 'default', fileName :new Date().getTime(), directory: 'Documents', }; // passing " type: 'default' " as extra param to put condition in the native code

Then used the "type" params in iOS Native code in below function like

*- (NSData) printToPDF:(NSInteger)_numberOfPages backgroundColor:(UIColor)_bgColor pdfType:(NSString)_pdfType

NSInteger pdfPageCount = self.numberOfPages; if([_pdfType isEqualToString:@"custom"]){ pdfPageCount = pdfPageCount - 1; } for ( int i = 0 ; i < pdfPageCount ; i++ ) await RNHTMLtoPDF.convert(options)