2010-01-25 19 views
5

पर फिट करने के लिए एचटीएमएल स्केल करने के लिए बताएं मैं एक HTML रिपोर्ट को पीडीएफ फ़ाइल में कनवर्ट करने के लिए abcPdf का उपयोग कर रहा हूं। पीडीएफ एक एकल परिदृश्य ए 4 पेज होना चाहिए।एबीसीपीडीएफ को एक पीडीएफ पेज

क्या आप जानते हैं कि पीडीएफ में एक पृष्ठ पर फिट करने के लिए HTML पृष्ठ को स्केल करने के लिए abcPdf को बताने का कोई तरीका है या नहीं? मैंने Magnify() method का उपयोग करने की कोशिश की, और यह सामग्री को स्केल करता है लेकिन फिर भी इसे पृष्ठों में तोड़ देता है, भले ही यह एक पृष्ठ पर फिट होगा। मैं थोड़ी देर के लिए इस पर अपने सिर खरोंच कर रहा हूं, और सोच रहा हूं कि किसी ने इसे किया है या नहीं।

public byte[] UrlToPdf(string url, PageOrientation po) 
{ 
    using (Doc theDoc = new Doc()) 
    { 
     // When in landscape mode: 
     // We use two transforms to apply a generic 90 degree rotation around 
     // the center of the document and rotate the drawing rectangle by the same amount. 
     if (po == PageOrientation.Landscape) 
     { 
      // apply a rotation transform 
      double w = theDoc.MediaBox.Width; 
      double h = theDoc.MediaBox.Height; 
      double l = theDoc.MediaBox.Left; 
      double b = theDoc.MediaBox.Bottom; 
      theDoc.Transform.Rotate(90, l, b); 
      theDoc.Transform.Translate(w, 0); 

      // rotate our rectangle 
      theDoc.Rect.Width = h; 
      theDoc.Rect.Height = w; 

      // To change the default orientation of the document we need to apply a rotation to the root page object. 
      //By doing this we ensure that every page in the document is viewed rotated. 
      int theDocID = Convert.ToInt32(theDoc.GetInfo(theDoc.Root, "Pages")); 
      theDoc.SetInfo(theDocID, "/Rotate", "90"); 
     } 

     theDoc.HtmlOptions.PageCacheEnabled = false; 
     theDoc.HtmlOptions.AddForms = false; 
     theDoc.HtmlOptions.AddLinks = false; 
     theDoc.HtmlOptions.AddMovies = false; 
     theDoc.HtmlOptions.FontEmbed = false; 
     theDoc.HtmlOptions.UseResync = false; 
     theDoc.HtmlOptions.UseVideo = false; 
     theDoc.HtmlOptions.UseScript = false; 
     theDoc.HtmlOptions.HideBackground = false; 
     theDoc.HtmlOptions.Timeout = 60000; 
     theDoc.HtmlOptions.BrowserWidth = 0; 
     theDoc.HtmlOptions.ImageQuality = 101; 

     // Add url to document. 
     int theID = theDoc.AddImageUrl(url, true, 0, true); 
     while (true) 
     { 
      if (!theDoc.Chainable(theID)) 
       break; 
      theDoc.Page = theDoc.AddPage(); 
      theID = theDoc.AddImageToChain(theID); 
     } 
     //Flattening the pages (Whatever that means) 
     for (int i = 1; i <= theDoc.PageCount; i++) 
     { 
      theDoc.PageNumber = i; 
      theDoc.Flatten(); 
     } 

     return theDoc.GetData(); 
    } 
} 

उत्तर

5

तो यहाँ मैं यह कैसे हल किया है:

यहाँ कोड मैं इस समय का उपयोग कर रहा है।

सबसे पहले, मैं HTML पृष्ठ के ऊंचाई पीडीएफ पैदा विधि को पास करने की जरूरत है, तो मैं पीडीएफ एड होने के लिए पृष्ठ पर इस कहा:

<asp:HiddenField ID="hfHeight" runat="server" /> 

और कोड में पीछे :

protected void Page_Init(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     string scriptKey = "WidhtHeightForPdf"; 
     if (!Page.ClientScript.IsClientScriptBlockRegistered(scriptKey)) 
     { 
      StringBuilder sb = new StringBuilder(); 
      sb.AppendLine("<script>") 
       .AppendLine("document.getElementById('" + hfHeight.ClientID + "').value = document.body.clientHeight;") 
       .AppendLine("</script>"); 

      Page.ClientScript.RegisterStartupScript(typeof(Page), scriptKey, sb.ToString()); 
     } 
    } 
} 

अब, जब मैं पीडीएफ जेनरेटिंग विधि कहता हूं तो मैं इसे HTML की ऊंचाई पास कर सकता हूं।

int intHTMLWidth = height.Value * Convert.ToInt32(theDoc.Rect.Width/theDoc.Rect.Height); 

और फिर या तो theDoc की HtmlOptions के माध्यम से BrowserWidth पैरामीटर निर्दिष्ट: एक बार मैं ऊंचाई है यह पीडीएफ "व्यूपोर्ट" ऐसी है कि ऊंचाई पीडीएफ पृष्ठ पर फिट बैठता है की चौड़ाई की गणना के सभी बात है

theDoc.HtmlOptions.BrowserWidth = intHTMLWidth; 

या जब theDoc को यूआरएल जोड़ने:

संपादित करें: इस सवाल को हल करती है, इसलिए मैं जा रहा हूँ इसे एक उत्तर के रूप में चिह्नित करने के लिए। अब करने के लिए अगली बात एचटीएमएल की चौड़ाई और ऊंचाई के आधार पर प्रोटेट या लैंडस्केप मोड में पीडीएफ बना रही है, ताकि पीडीएफ पेज पर अधिकतम स्थान का उपयोग किया जा सके।

+0

क्या आपने इसे पूरा किया और इसे पूरी तरह से काम किया? –

+0

हाय माइक या बीन किसी के पास नीचे दिए गए मुद्दे के बारे में विचार है http://stackoverflow.com/questions/22069825/abcpdf-not-showing-full-table-data – SivaRajini

1

इसमें कुछ समय सरल

/// <summary> 
    /// Calculate the height of given html 
    /// </summary> 
    /// <param name="html"></param> 
    /// <returns></returns> 
    public int CalculateHeight(string html) 
    { 
     int id = _Document.AddImageHtml(html); 
     int height = (int)(_Document.GetInfoInt(id, "ScrollHeight") * PixelToPointScale); 
     _Document.Delete(id); 
     return height; 
    } 

[संपादित करें] खैर scrollHeight ver8 हालांकि इस

private int AddImageHtml(string html) 
    { 
     try 
     { 
      return _Document.AddImageHtml("<div id='pdfx-div-pdf-frame' class='abcpdf-tag-visible' style='abcpdf-tag-visible: true; border: 1px solid red'>" + html + "</div>"); 
     } 
     catch (Exception ex) 
     { 
      throw new Exception(html, ex); 
     } 
    } 

    private double GetElementHeight(int id) 
    { 
     abcpdf.XRect[] tagRects = _Document.HtmlOptions.GetTagRects(id); 
     string[] tagIds = _Document.HtmlOptions.GetTagIDs(id); 

     for (int i=0;i<tagRects.Length;i++) 
     { 
      abcpdf.XRect rect = tagRects[i]; 
      string tagId = tagIds[i]; 
      if (string.Equals(tagId, "pdfx-div-pdf-frame", StringComparison.CurrentCultureIgnoreCase)) 
      { 
       return rect.Height; 
      } 
     } 
     return -1; 
    } 
0

काम करता है आप उपयोग कर रहे 'छिपकली' इंजन मामले में साथ विफल रहता है, इस इंजन नहीं है हो सकता है 'GetInfoInt' का समर्थन करें ताकि हमें ऊंचाई प्राप्त करने के लिए कुछ जावास्क्रिप्ट लिखने की आवश्यकता हो। ऊंचाई निर्धारित करने के लिए पहले एक डमी प्रस्तुत करें और फिर इस ऊंचाई को मूल AbcDoc पर सेट करें।

using (var tempDoc = new Doc()) 
       { 
        tempDoc.HtmlOptions.Engine = EngineType.Gecko; 
        tempDoc.HtmlOptions.Media = MediaType.Print; 
        tempDoc.HtmlOptions.UseScript = true; 
        if (width.HasValue) 
         tempDoc.HtmlOptions.BrowserWidth = width.Value; 

        tempDoc.HtmlOptions.OnLoadScript = " window.onbeforeprint = function() { document.documentElement.abcpdf = Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);}"; 
        int theTempID = tempDoc.AddImageHtml(htmlData); 

        int height = Convert.ToInt32(tempDoc.HtmlOptions.GetScriptReturn(theTempID)); 

        tempDoc.Clear(); 
        tempDoc.Dispose(); 

        theDoc.MediaBox.Height = height; 
        theDoc.Rect.String = theDoc.MediaBox.String; 
        theDoc.AddImageHtml(htmlData); 
       } 
संबंधित मुद्दे