¿Cómo exportar una página web a PDF con solo pasar la URL de parámetro?

En este post veremos una tarea que es muy común en el desarrollo de sitios web, cómo exportar una página web  o HTML a PDF. El código es muy sencillo, solo se requiere de 4 líneas de código y listo. Al final del artículo puedes encontrar un video que te lleva paso a paso de cómo exportar una página web a PDF. Bueno pasamos al ejemplo


  1. Vas a Estrada WebGroup, HTML a PDF y seleccionas probar para generar tu API
  2. Creas un sitio web nuevo en Visual Studio
  3. Agregas la referencia del servicio web (http://produccion.developerji.com/ConvertirHtmlaPDF.asmx), como se muestra a continuación.
    convertir html a pdf en mi sitio web      convertir html a pdf con asp.net

    servicio web para convertir html a pdf
     
  4. Agregas una página al sitio y a esta le agregas un botón, que sería el que invoque la conversión de la página a PDF. El HTML queda de la siguiente manera:
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Button ID="bntConvertPDF" runat="server" Text="Convertir a PDF" />
        </div>
        </form>
    </body>
    </html>
    
  5. Creas el código de servidor en el evento clic del botón y este quedaría de la siguiente manera:
     Protected Sub bntConvertPDF_Click(sender As Object, e As EventArgs) Handles bntConvertPDF.Click
            'Dirección fisica donde se guardara el pdf 
            Dim sFile As String = Server.MapPath("~/Cliente/pdfdemo.pdf")
            'Llamada al servicio web  
            Dim servicio As New ConvertPDF.ConvertirHtmlaPDFSoapClient
            'Descargo el PDF y se guarda en el servidor del cliente  
            'sFile: la dirección donde se guarda el PDF. En el servicio web servicio.ConvertirUrl_PDF,  
            ' se pasa por parametro la url a convertir, el nombre de usuario y el API que se genera cuando  
            'el usuario se registra en developerji.com 
            File.WriteAllBytes(sFile, servicio.ConvertirUrl_PDF("http://es.wikipedia.org", "jiestrada", "qF4OxILMDYXY6vQ"))
            'Por ultimo redirigimos a donde se encuentra el PDF
            Response.Redirect("~/Cliente/pdfdemo.pdf")
        End Sub
  6. Por ultimo abres el web.config  y en <binding debe quedar de la siguiente manera, no modifique el parámetro name, solo agrega lo que falte y listo, ejecuta tu sitio web.
     <binding name="ConvertirHtmlaPDFSoap" openTimeout="00:12:00" receiveTimeout="00:12:00" sendTimeout="00:12:00" maxBufferPoolSize="2097152" maxReceivedMessageSize="2097152"/>

 

Artículos relacionados
16 Comentarios
  • Cisco Comentar
    Thursday, March 8, 2018

    A person essentially lend a hand to make significantly posts I would state. That is the very first time I frequented your web page and thus far? I surprised with the research you made to create this particular publish amazing. Magnificent job!

  • Fregoso Comentar
    Friday, March 16, 2018

    obviously like your web site but you have to check the spelling on quite a few of your posts. Many of them are rife with spelling problems and I to find it very bothersome to inform the reality nevertheless I'll definitely come again again.

  • Trowbridge Comentar
    Tuesday, March 27, 2018

    When I originally commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is added I get three emails with the same comment. Is there any way you can remove me from that service? Thank you!

  • Tran Comentar
    Wednesday, March 28, 2018

    intentalo varias veces si funciona

  • Frederic Comentar
    Friday, March 30, 2018

    Yo agarre vip no es la gran cosa. whatsapp me gusto mas la gratis que la vip

  • Banks Comentar
    Thursday, April 5, 2018

    inscreve no meu canal

  • Winder Comentar
    Tuesday, April 10, 2018

    Hi there, every time i used to check web site posts here early in the morning, for the reason that i love to gain knowledge of more and more.

  • Knorr Comentar
    Wednesday, April 25, 2018

    Keep on working, great job!

  • Whitmore Comentar
    Wednesday, April 25, 2018

    Hello just wanted to give you a brief heads up and let you know a few of the images aren't loading correctly. I'm not sure why but I think its a linking issue. I've tried it in two different internet browsers and both show the same results.

  • McHale Comentar
    Wednesday, April 25, 2018

    Good answer back in return of this issue with real arguments and explaining everything regarding that.

  • Tauchert Comentar
    Thursday, April 26, 2018

    Pretty element of content. I simply stumbled upon your weblog and in accession capital to claim that I acquire in fact enjoyed account your blog posts. Any way I'll be subscribing in your augment or even I success you get admission to consistently fast.

  • Farrow Comentar
    Friday, April 27, 2018

    I will right away grab your rss as I can't in finding your e-mail subscription link or e-newsletter service. Do you've any? Please permit me recognise in order that I may subscribe. Thanks.

  • Honeycutt Comentar
    Tuesday, May 1, 2018

    Hi, i think that i saw you visited my website so i came to “return the favor”.I'm attempting to find things to improve my web site!I suppose its ok to use a few of your ideas!!

  • Mcnamee Comentar
    Thursday, May 17, 2018

    This blog was... how do you say it? Relevant!! Finally I've found something which helped me. Thanks a lot!

  • Mcdaniels Comentar
    Sunday, May 27, 2018

    Hi! Would you mind if I share your blog with my zynga group? There's a lot of people that I think would really enjoy your content. Please let me know. Thanks

  • Miles Comentar
    Monday, May 28, 2018

    I am extremely impressed with your writing skills and also with the layout on your blog. Is this a paid theme or did you modify it yourself? Anyway keep up the excellent quality writing, it's rare to see a great blog like this one nowadays.

Enviame un comentario