0

Does anybody know how to make a print page button print a pdf document?

At the moment i'm using

<a href="javascript:window.print()" class="print_it" title="Print page">Print Page</a>

Obviously that just prints the page though. I have had to create pdf's for each page and thought it would be easier just to print the pdf instead of the page (Cross browser printing styles is kinda sucking ;).

Any ideas?

Nik
  • 671
  • 1
  • 8
  • 27
  • 1
    How are you displaying the PDF? Are they links or open in the web browser? – Shoban Nov 25 '10 at 04:01
  • possible duplicate of [Silent print a embedded PDF](http://stackoverflow.com/questions/975652/silent-print-a-embedded-pdf) – JohnFx Nov 25 '10 at 04:08
  • The pdf is just a link to download. Basically I want - when a user clicks print page it prints the pdf instead. @JohnFx I don't want to silently print. – Nik Nov 25 '10 at 06:24
  • This seems to be a rather difficult problem to solve. You might find this question question useful: http://stackoverflow.com/questions/1686280/convert-html-having-javascript-to-pdf-using-java-javascript – mynameisnotallowed Aug 07 '12 at 14:04

2 Answers2

0

There is no standard way to print anything in PDF in any browser, such as on the Windows platform. On the Mac, there is always an option to print something as a PDF file, so regular printing will do.

nonopolarity
  • 146,324
  • 131
  • 460
  • 740
0

I suggest you use Itextsharp. If you are using asp.net c#, this code works for you. Runs in the server side though. You can just put the html inside a panel to make it readable in the server.

    /// import these namespaces 
    using System.IO;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using iTextSharp.text.html.simpleparser;
    using System.Web.Services;
    using System.Text;

    /// Call this method whenever you need to convert 
    /// the html content inside the panel which runs in the server side. 
    [WebMethod]
    public void ConvertHtmlStringToPDF()
    {
        StringBuilder sb = new StringBuilder(); 
        StringWriter tw = new StringWriter(sb); 
        HtmlTextWriter hw = new HtmlTextWriter(tw); 
        pnlPDF.RenderControl(hw);
        string htmlDisplayText = sb.ToString(); 

        Document document = new Document();
        MemoryStream ms = new MemoryStream();
        PdfWriter writer = PdfWriter.GetInstance(document, ms);
        StringReader se = new StringReader(htmlDisplayText);
        HTMLWorker obj = new HTMLWorker(document);
        document.Open();
        obj.Parse(se);
        // step 5: we close the document
        document.Close();
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment; filename=report.pdf");
        Response.ContentType = "application/pdf";
        Response.Buffer = true;
        Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
        Response.OutputStream.Flush();
        Response.End();
    }
Carls Jr.
  • 3,088
  • 7
  • 37
  • 57