h5打印Html


今天有個客戶提了個需求,有個較復雜的頁面需要導出為pdf,如果使用現有的生成pdf,再通過企業微信替換通訊錄比較復雜,於是直接調用系統打印功能實現

找到一篇比較簡單的解決方案

https://blog.csdn.net/alokka/article/details/75103322

1、添加js代碼

<script language="javascript">
function preview(oper)
{
if (oper < 10){
bdhtml=window.document.body.innerHTML;//獲取當前頁的html代碼
sprnstr="<!--startprint"+oper+"-->";//設置打印開始區域
eprnstr="<!--endprint"+oper+"-->";//設置打印結束區域
prnhtml=bdhtml.substring(bdhtml.indexOf(sprnstr)+18); //從開始代碼向后取html
prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));//從結束代碼向前取html
window.document.body.innerHTML=prnhtml;
window.print();
window.document.body.innerHTML=bdhtml;
} else {
window.print();
}
}
</script>

2、需要打印的部分,使用如下代碼包裹,多個打印區域可以設置多個startprint2...

<!--startprint1-->

<!--打印內容開始-->
<div id=sty>
    ...
</div>
<!--打印內容結束-->

<!--endprint1-->

3、打印

 <a onclick="preview(1);"style="float:right;position:relative">打印</a>

4、效果

 5、如果使用輸出PDF方式,可以是使用PDFHelper

using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace Utilities
{
    /// <summary>
    /// PDF文檔操作類
    /// </summary>
    //------------------------------------調用--------------------------------------------
    //PDFHelper pdf = new PDFHelper();
    //pdf.Open(new FileStream(path, FileMode.Create));
    //pdf.SetBaseFont(@"C:\Windows\Fonts\SIMHEI.TTF");
    //pdf.AddParagraph("測試文檔(生成時間:" + DateTime.Now + ")", 15, 1, 20, 0, 0);
    //pdf.Close();
    //-------------------------------------------------------------------------------------
    public class PDFHelper
    {
        #region 構造函數
        /// <summary>
        /// 構造函數
        /// </summary>
        public PDFHelper()
        {
            rect = PageSize.A4;
            document = new Document(rect);
        }

        /// <summary>
        /// 構造函數
        /// </summary>
        /// <param name="type">頁面大小(如"A4")</param>
        public PDFHelper(string type)
        {
            SetPageSize(type);
            document = new Document(rect);
        }

        /// <summary>
        /// 構造函數
        /// </summary>
        /// <param name="type">頁面大小(如"A4")</param>
        /// <param name="marginLeft">內容距左邊框距離</param>
        /// <param name="marginRight">內容距右邊框距離</param>
        /// <param name="marginTop">內容距上邊框距離</param>
        /// <param name="marginBottom">內容距下邊框距離</param>
        public PDFHelper(string type, float marginLeft, float marginRight, float marginTop, float marginBottom)
        {
            SetPageSize(type);
            document = new Document(rect, marginLeft, marginRight, marginTop, marginBottom);
        }
        #endregion

        #region 私有字段
        private Font font;
        private Rectangle rect;   //文檔大小
        private Document document;//文檔對象
        private BaseFont basefont;//字體
        #endregion

        #region 設置字體
        /// <summary>
        /// 設置字體
        /// </summary>
        public void SetBaseFont(string path)
        {
            basefont = BaseFont.CreateFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        }

        /// <summary>
        /// 設置字體
        /// </summary>
        /// <param name="size">字體大小</param>
        public void SetFont(float size)
        {
            font = new Font(basefont, size);
        }
        #endregion

        #region 設置頁面大小
        /// <summary>
        /// 設置頁面大小
        /// </summary>
        /// <param name="type">頁面大小(如"A4")</param>
        public void SetPageSize(string type)
        {
            switch (type.Trim())
            {
                case "A4":
                    rect = PageSize.A4;
                    break;
                case "A8":
                    rect = PageSize.A8;
                    break;
            }
        }
        #endregion

        #region 實例化文檔
        /// <summary>
        /// 實例化文檔
        /// </summary>
        /// <param name="os">文檔相關信息(如路徑,打開方式等)</param>
        public void GetInstance(Stream os)
        {
            PdfWriter.GetInstance(document, os);
        }
        #endregion

        #region 打開文檔對象
        /// <summary>
        /// 打開文檔對象
        /// </summary>
        /// <param name="os">文檔相關信息(如路徑,打開方式等)</param>
        public void Open(Stream os)
        {
            GetInstance(os);
            document.Open();
        }
        #endregion

        #region 關閉打開的文檔
        /// <summary>
        /// 關閉打開的文檔
        /// </summary>
        public void Close()
        {
            document.Close();
        }
        #endregion

        #region 添加段落
        /// <summary>
        /// 添加段落
        /// </summary>
        /// <param name="content">內容</param>
        /// <param name="fontsize">字體大小</param>
        public void AddParagraph(string content, float fontsize)
        {
            SetFont(fontsize);
            Paragraph pra = new Paragraph(content, font);
            document.Add(pra);
        }

        /// <summary>
        /// 添加段落
        /// </summary>
        /// <param name="content">內容</param>
        /// <param name="fontsize">字體大小</param>
        /// <param name="Alignment">對齊方式(1為居中,0為居左,2為居右)</param>
        /// <param name="SpacingAfter">段后空行數(0為默認值)</param>
        /// <param name="SpacingBefore">段前空行數(0為默認值)</param>
        /// <param name="MultipliedLeading">行間距(0為默認值)</param>
        public void AddParagraph(string content, float fontsize, int Alignment, float SpacingAfter, float SpacingBefore, float MultipliedLeading)
        {
            SetFont(fontsize);
            Paragraph pra = new Paragraph(content, font);
            pra.Alignment = Alignment;
            if (SpacingAfter != 0)
            {
                pra.SpacingAfter = SpacingAfter;
            }
            if (SpacingBefore != 0)
            {
                pra.SpacingBefore = SpacingBefore;
            }
            if (MultipliedLeading != 0)
            {
                pra.MultipliedLeading = MultipliedLeading;
            }
            document.Add(pra);
        }
        #endregion

        #region 添加圖片
        /// <summary>
        /// 添加圖片
        /// </summary>
        /// <param name="path">圖片路徑</param>
        /// <param name="Alignment">對齊方式(1為居中,0為居左,2為居右)</param>
        /// <param name="newWidth">圖片寬(0為默認值,如果寬度大於頁寬將按比率縮放)</param>
        /// <param name="newHeight">圖片高</param>
        public void AddImage(string path, int Alignment, float newWidth, float newHeight)
        {
            Image img = Image.GetInstance(path);
            img.Alignment = Alignment;
            if (newWidth != 0)
            {
                img.ScaleAbsolute(newWidth, newHeight);
            }
            else
            {
                if (img.Width > PageSize.A4.Width)
                {
                    img.ScaleAbsolute(rect.Width, img.Width * img.Height / rect.Height);
                }
            }
            document.Add(img);
        }
        #endregion

        #region 添加鏈接、點
        /// <summary>
        /// 添加鏈接
        /// </summary>
        /// <param name="Content">鏈接文字</param>
        /// <param name="FontSize">字體大小</param>
        /// <param name="Reference">鏈接地址</param>
        public void AddAnchorReference(string Content, float FontSize, string Reference)
        {
            SetFont(FontSize);
            Anchor auc = new Anchor(Content, font);
            auc.Reference = Reference;
            document.Add(auc);
        }

        /// <summary>
        /// 添加鏈接點
        /// </summary>
        /// <param name="Content">鏈接文字</param>
        /// <param name="FontSize">字體大小</param>
        /// <param name="Name">鏈接點名</param>
        public void AddAnchorName(string Content, float FontSize, string Name)
        {
            SetFont(FontSize);
            Anchor auc = new Anchor(Content, font);
            auc.Name = Name;
            document.Add(auc);
        }
        #endregion
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM