【问题标题】:Need to generate PDF and that in "3 X 4" format需要生成 PDF 和“3 X 4”格式
【发布时间】:2018-04-26 05:57:58
【问题描述】:

是否可以生成“3 X 4”形式的pdf?

我尝试了如下代码但没有生成PDF,我从here下载了JSPDF.min

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="js/jspdf.min.js"></script>
<script>       
    var doc = new jsPDF();
    var specialElementHandlers = {
        '#editor': function (element, renderer) {
            return true;
        }
    };
    $('#cmd').click(function () {
        doc.fromHTML($('#content').html(), 15, 15, {
            'width': 170,
            'elementHandlers': specialElementHandlers
        });
        doc.save('sample-file.pdf');
    });

</script>

html代码

<div id="content">
<h3>Hello, this is a H3 tag</h3>    
<p>a pararaph</p>
</div>
<div id="editor"></div>
<button id="cmd">generate PDF</button>

这是我页面中的代码,我无法创建 PDF,以及如何生成 PDF 如何使其以 3X4 格式显示?

OI 已从 http://jsfiddle.net/5ud8jkvf/11511/ 获得此代码的参考,并且在这里正确生成了 PDF,我缺少什么?

【问题讨论】:

    标签: javascript c# pdf pdf-generation jspdf


    【解决方案1】:

    我使用过 iTextSharp 并且已经使用它。以下是我的以下代码

    using iTextSharp.text;
    using iTextSharp.text.pdf;
    string shippingaddress = "My name is khan <br/>";
    shippingaddress += "And I am not a terrorist<br/>";
            using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
            {            
                //Below code defines the PDF generate Size
                Rectangle newRect = new Rectangle(0, 0, 240, Convert.ToSingle(138.6));
                Document document = new Document(newRect,10f,10f,10f,10f);
                Document.Compress = true;
                PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
                document.Open();
                Font boldFont = new Font(Font.FontFamily.HELVETICA, 12,Font.BOLD);
                Paragraph para = new Paragraph("Header", boldFont);
                document.Add(para);
                string text = shippingaddress.Replace("<br/>", "\n");
                Paragraph paragraph = new Paragraph();
                paragraph.SpacingBefore = 2;
                paragraph.SpacingAfter = 2;
                paragraph.Leading = 12;
                paragraph.Alignment = Element.ALIGN_LEFT;
                paragraph.Font = FontFactory.GetFont(FontFactory.HELVETICA, 12f,BaseColor.BLACK);
                paragraph.Add(text);
                document.Add(paragraph);
                document.Close();
                byte[] bytes = memoryStream.ToArray();
                memoryStream.Close();
                Response.Clear();
                Response.ContentType = "application/pdf";
                string pdfName = "Movie Name";
                Response.AddHeader("Content-Disposition", "inline; filename=" + pdfName + ".pdf");    
                Response.ContentType = "application/pdf";
                Response.Buffer = true;   
                Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
                Response.BinaryWrite(bytes);
                Response.End();
                Response.Close();    
            }
    

    通过这段代码,我能够创建一个 3X4 格式的 PDF。 Mat这个答案将是完整的帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-10
      • 2019-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-23
      相关资源
      最近更新 更多