?
Solved

Save HTML webpage to PDF via .NET

Posted on 2013-12-02
3
Medium Priority
?
2,035 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 39691785
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
ID: 39692051
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 1500 total points
ID: 39692206
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

762 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