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

¿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"/>

Compartir

Comentarios (9)

  • Carlos Responder

    Muy bueno el post. Tengo dos dudas . Se puede implantar desde un entorno alojada en una intranet?. otra duda es si la api ley de la opcion probar tiene caducidad?

    Wednesday, November 23, 2016 12:16 PM
  • DHB Responder

    Pregunta puedo implementarlo en c#?? y cual seria el ejemplo

    Wednesday, November 23, 2016 12:50 PM
  • Stansbury Responder

    Heya! I just wanted to ask if you ever have any issues with hackers? My last blog (wordpress) was hacked and I ended up losing months of hard work due to no back up. Do you have any solutions to stop hackers?

    Wednesday, May 31, 2017 11:25 AM
  • Leeson Responder

    Awesome issues here. I am very happy to peer your post. Thanks so much and I'm taking a look ahead to contact you. Will you please drop me a mail?

    Sunday, June 4, 2017 10:02 PM
  • McWilliams Responder

    Hello! This is my 1st comment here so I just wanted to give a quick shot out and say I really enjoy reading your posts. Can you suggest any other blogs/websites/forums that go over the same topics? Thank you so much!

    Wednesday, June 14, 2017 8:41 AM
  • Torreggiani Responder

    Antibiótico bem como corticosteróide bom contra ?

    Tuesday, September 12, 2017 1:08 AM
  • Tuckett Responder

    Hello there! I know this iss somewhat off topic but I was wondering which blog platform are you using for this site? I'm getting tired of Wordpress because I've had issues with hackers and I'm looking at alternatives forr another platform. I would be greeat if yyou could point me in the direction of a good platform.

    Wednesday, September 13, 2017 1:51 PM
  • Fitzhardinge Responder

    Neede? to create you that litt?e observation so as t? give thank? over ag?in with y?ur remarjable opinions ?ou've featured h??e. It w?s ?ertainly wonderfully generous of people likke ?ou to give publicly ?xactly what a f?w people m?ght have made avail?ble as ?n e-book tto mak? ?ome money ?n their o?n, p?rticularly c?nsidering that you c?uld h?ve tried it if you e?er wanted. Tholse secrets iin ?ddition ?orked t? become a fantastic way to be ?ertain that ?omeone el?e have similar dreams juat ?? myy personal own to realize ? little mre with regard t? this p?oblem. I b?lieve there a?e many mo?e fun sessions up fr?nt fo? people ?ho discover yopur website. I ?ould like to express thanks to y?u for rescuing me from thi? particuloar trouble. ?ight ?fter exploring through the online w?rld and coming ac?oss techniques ?hich are not helpful, ? thought m? life was over. Beingg alivce ?ithout the solutions to the issues yo? ha?? solved ?y m?ans of your ent?re site iis a crucial ?ase, and the kind that could hav? negatively damaged my entire careerr ?f I h?d not ?om acroas thhe website. Youur understanding ?nd kindness in touching ?ll thing? w?s vital. I'm not sure wh?t I would ha?e done if I had not encountered s?ch a stuff like th?s. ? ?an now look forward to my future. ?hanks a l?t ?ery much f?r our professional and efective he?p. I won't hesitate t? endorse y?ur web ?age t? any person ?ho needs counseoling on this topic. I really w?nted to m?ke a bri?f remark to express gratitude to ?ou for the nice tactics yoou are giving on th?s website. My ?articularly ?ong internet search has at the end ?f thhe day be?n compensated with awesome ideas t? wrjte a?out with my company. I wou?? declare that w? website visitors are extremely fortunate too be in a notable community ?ith so many outstanding professionals with great tricks. I feel extremely happy to have s?en your website ?age and ?ook forward to plenty of mor? thrrilling moments reading here. Thank ?ou once again for everything. Thanks so much for providing individuals ?ith an extraordinarily spectacular possiblity t? rea? critical reviews f?om here. It can bee very seful ?nd aas we?l , packed with amusemeht fo? me and my office peers to visit y?ur site at m?nimum 3 t?me? in one week to s?e the l?test issues you h?ve. And indeed, we're usually contented ?ith all the attractive ?oints yo? give. Certain 3 ar?as inn thi? post a?e unquestionably the mo?t impressive ?e have ?ver ha?. I ?ould like to point ouut my love for you? generosity supporting women ?h? shoulkd hav? help w?th the niche. Your very own dedication to passing the message across ?as exceptioonally im?ortant and hav? specifically helped associates lioke me to attain their objectives. ?he u?eful publication implies ? ?ot to m? ?nd additionally to my mates. Re?ards; fr?m all of ?s. I in addtion to m? friends came analyzing the best things fr?m your web blog then aat once I had a terrible sujspicion ? h?? not expressed respeht to the web blog owner for them. ?ll the people ended up so warmed to learn all of them and have now ?ctually been tapping into those things. We appreciate ??u turning out to bbe s? thoughtful ?nd also fo? choosing ce?tain nice subject areas millions of indviduals ?re really desiros to learn about. My very own honest apologies f?r not ?aying thanks to sooner. I'm ?lso writing t? let you understand what a superb discovery my girl enjoyed reading ?our webblog. She discovered ?o many issues, which inclu?e what it's l?ke to h??e an amazing giving style t? ha?e men ?nd women eaxily fullyy grasp choseen multifaceted topics. ??u undoubtedly did mor? than our own expectations. Thank yoou for providing those invaluable, healthy, informative ?nd also unique guidance ?n the topic to Mary. ? precisely had t? thank you very much again. I do not kno? tthe things I co?ld p?ssibly h??e handled wothout these thoughts pro?ided by you oover my concern. ?h?s was the difficult crisis iin m? position, butt being ?ble to vi?w your specialized approach y?u hanndled that t??k me to cry fo? delight. N?w i'm thankful f?r yourr ?ork and thus beieve ?ou are aware of ? ?reat job y?u were providing teaching other individduals ?sing your blog post. ? am cert?in you ha?? nevrr come across aall of us. My husband and i fe?t so thankful wh?n Raymond co?ld do hi? web research ?ith the precious recommendations he was given out ?f your blog. It i? now and again perplexing just t?? pos?ibly be givin freely techniques that m?ny the others could ha?e been m?king money from. S? w? discover we have

    Wednesday, September 13, 2017 11:01 PM
  • Cushing Responder

    Tremendous issues here. I'm very happy to peer your article. Thank you a lot and I'm having a look forward to contact you. Will you please drop me a e-mail?

    Tuesday, September 19, 2017 4:42 PM

Animate deja un comentario!!!

Eres un humano?

Últimos Tweets

Contáctanos

  • Dirección: Plutarco Elias Calles 243, Granjas México, Iztacalco, DF.

  • Teléfono:55-2712-3820 o 55-4164-9484

  • Email: info@developerji.com