Solved

Save HTML webpage to PDF via .NET

Posted on 2013-12-02
3
1,843 Views
Last Modified: 2013-12-06
Can anyone recommend a decent open source application that can be used within VB.net that can save a completed webpage to a PDF file?
0
Comment
Question by:Cmitch
  • 2
3 Comments
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
Comment Utility
hi..
The following code shows how to convert a web page, raw HTML code, and a local HTML file:

using System;
using System.IO;

public class PdfcrowdTest
{
  static void Main() {
    try
    {
      FileStream fileStream;  

      // create an API client instance
      pdfcrowd.Client client = new pdfcrowd.Client("username", "apikey");

      // convert a web page and save the PDF to a file
      fileStream = new FileStream("google_com.pdf", FileMode.CreateNew);
      client.convertURI("http://www.google.com", fileStream);
      fileStream.Close();

      // convert an HTML string and store the PDF into a memory stream
      MemoryStream memStream = new MemoryStream();
      string html = "<head></head><body>My HTML Layout</body>";
      client.convertHtml(html, memStream);

      // convert an HTML file
      fileStream = new FileStream("file.pdf", FileMode.CreateNew);
      client.convertFile("c:/local/file.html", fileStream);
      fileStream.Close();

      // retrieve the number of tokens in your account
      int ntokens = client.numTokens();
    }
    catch(pdfcrowd.Error why) {
      System.Console.WriteLine(why.ToString());
    }
  }
}

HTML to PDF in ASP.NET - C#

The following code converts a web page using C# ASP.NET and sends the generated PDF as an HTTP response:

<%-- file: PdfGenerator.aspx --%>
<%@ Page Language="C#" CodeFile="PdfGenerator.aspx.cs" Inherits="Website.PdfGenerator"
         AutoEventWireup="true" %>


// file: PdfGenerator.aspx.cs
using System;
using System.IO;

namespace Website
{
  public partial class PdfGenerator : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response;
      try
      {
          // create an API client instance
          pdfcrowd.Client client = new pdfcrowd.Client("username", "apikey");

          // convert a web page and write the generated PDF to a memory stream
          MemoryStream Stream = new MemoryStream();
          client.convertURI("http://www.google.com", Stream);

          // set HTTP response headers
          Response.Clear();
          Response.AddHeader("Content-Type", "application/pdf");
          Response.AddHeader("Cache-Control", "no-cache");
          Response.AddHeader("Accept-Ranges", "none");
          Response.AddHeader("Content-Disposition", "attachment; filename=google_com.pdf");

          // send the generated PDF
          Stream.WriteTo(Response.OutputStream);
          Stream.Close();
          Response.Flush();
          Response.End();
      }
      catch(pdfcrowd.Error why)
      {
          Response.Write(why.ToString());
      }
    }
  }
}

You can also convert raw HTML code, just use the convertHtml() method instead of convertURI():

    client.convertHtml("<head></head><body>My HTML Layout</body>", Stream);



The API lets you also convert a local HTML file:

    client.convertFile("c:/MyLayout.html", Stream);

HTML to PDF in ASP.NET - Visual Basic

The following code converts a web page using Visual Basic ASP.NET and sends the generated PDF as an HTTP response:

<%-- file: PdfGenerator.aspx --%>
<%@ Page Language="VB" CodeFile="PdfGenerator.aspx.vb" Inherits="Website.PdfGenerator"
         AutoEventWireup="true" %>


' file: PdfGenerator.aspx.vb
Imports System
Imports System.IO

Namespace Website
  Public Partial Class PdfGenerator
      Inherits System.Web.UI.Page
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        Dim Response As System.Web.HttpResponse = System.Web.HttpContext.Current.Response
        Try
            ' create an API client instance
            Dim client As New pdfcrowd.Client("username", "apikey")

            ' convert a web page and write the generated PDF to a memory stream
            Dim Stream As New MemoryStream
            client.convertURI("http://www.google.com", Stream)

            ' set HTTP response headers
            Response.Clear()
            Response.AddHeader("Content-Type", "application/pdf")
            Response.AddHeader("Cache-Control", "no-cache")
            Response.AddHeader("Accept-Ranges", "none")
            Response.AddHeader("Content-Disposition", "attachment; filename=google_com.pdf")

            ' send the generated PDF
            Stream.WriteTo(Response.OutputStream)
            Stream.Close()
            Response.Flush()
            Response.End()
        Catch why As pdfcrowd.Error
            Response.Write(why.ToString())
        End Try
      End Sub
  End Class
End Namespace


You can also convert raw HTML code, just use the convertHtml() method instead of convertURI():

    client.convertHtml("<head></head><body>My HTML Layout</body>", Stream)



The API lets you also convert a local HTML file:

    client.convertFile("c:/MyLayout.html", Stream)
0
 

Author Comment

by:Cmitch
Comment Utility
Is PDFcrowd restricted a certain amount of use?

I have been trying to use PDFcreator by PDFforge by using WebBrowser to render the webpage and PDFcreator to create the PDF but haven't been able to get this working.  Could you offer any advice?
0
 
LVL 11

Accepted Solution

by:
SAMIR BHOGAYTA earned 500 total points
Comment Utility
hi.. Can you try this code

We create an ABCpdf Doc object, add our URL and save. That's it!

[C#]
 Doc theDoc = new Doc();
 theDoc.AddImageUrl("http://www.google.com/");
 theDoc.Save(Server.MapPath("htmlimport.pdf"));
 theDoc.Clear();


[Visual Basic]
 Dim theDoc As Doc = New Doc()
 theDoc.AddImageUrl("http://www.google.com/")
 theDoc.Save(Server.MapPath("htmlimport.pdf"))
 theDoc.Clear()
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now