右侧的jspdf浮动项目

时间:2019-04-23 15:52:48

标签: jspdf

正在尝试将某些项目向右对齐,如果文本较长,则将其向左扩展

我已添加

///others

doc.setFontType("bold");
doc.setFontSize(9);
doc.setTextColor("#52575A")
doc.text(doc.internal.pageSize.getWidth()-70, 17, "Email: testhere@testingahugeandlengthyemail.com");
doc.text(doc.internal.pageSize.getWidth()-70, 22, "Tel: 83473487348347834734843");
doc.textWithLink('Website: https://test.test.com',doc.internal.pageSize.getWidth()-70 , 27, { url: 'https://ecommerce.soradius.co.ke' });
 doc.save('test.pdf')

上面的代码生成一个pdf boolean indexing

在上面的代码中,我将文本设置为距页面宽度70px的左侧。

当电子邮件上的文本增加时,其内容就会消失。如何将文本设置在右侧,并使其扩展到左侧。

我已经检查过enter image description here,但仍然无法正确对齐

2 个答案:

答案 0 :(得分:1)

要获得预期结果,请使用以下使用jspdf单位和doc.getTextWidth()的选项来计算电子邮件文本宽度

  1. 计算文字宽度与70的差值,并使用空格来避免文字被裁剪
  2. 使用文本宽度调整间距可以避免其他文本部分的间距

var content = document.getElementById('txtContent'),
button = document.getElementById('btnDownload');

function generatePDF(){
  var doc = new jsPDF('p', 'mm','a4');

doc.setFontType("bold");
doc.setFontSize(9);
doc.setTextColor("#52575A")
  let width = doc.internal.pageSize.getWidth()
  let emailText = "Email: testhere@testingahugeandlengthyemail.com"
  let txtWidth = doc.getTextWidth(emailText) > 70 ? doc.getTextWidth(emailText) - 70: 0;
  console.log("width", width, txtWidth)
doc.text(width - 70 - Math.ceil(txtWidth), 17, emailText);
doc.text(width-70, 22, "Tel: 83473487348347834734843");
doc.textWithLink('Website: https://test.test.com',width-70 , 27, { url: 'https://ecommerce.soradius.co.ke' });
 doc.save('test.pdf')
}

button.addEventListener('click', generatePDF);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js"></script>
<h1>jsPDF Demo</h1>

<textarea id="txtContent" cols="60" rows="15"></textarea>
<br />
<button id="btnDownload"> Download PDF </button>

codepen- https://codepen.io/nagasai/pen/yrxZOY

答案 1 :(得分:0)

Hope this answers helps for someone facing the same issue

    var str = 'Page 1 of 1'
    var pageSize = doc.internal.pageSize
    var pageHeight = pageSize.height ? pageSize.height : pageSize.getHeight()
    var pageWidth = doc.internal.pageSize.width || doc.internal.pageSize.getWidth();
    var txtwidth = doc.getTextWidth(str);               
    doc.text(str, pageWidth - (data.set`enter code here`tings.margin.left * 2) - txtwidth, pageHeight - 10)
[Got Expected Output][1] [1]: https://i.stack.imgur.com/jKAxi.png