Developer forum

Forum » Development » generate pdf from html

generate pdf from html


Reply
Hi.



Have you implemented any kind of pdfconverter which can be called from a custommodule to return a html page to pdf?



- Sune

Replies

 
Nicolai Høeg Pedersen
Reply

Hi Sune


 


You can do like this:


Dim pdf As New Dynamicweb.PDFClass("http://www.some.dk/some.aspx")


pdf.SaveToFile(HttpContext.Current.Server.MapPath("/Files/Filer/PDF/File.pdf"))


pdf.Dispose()


 


Be aware, that DNS can be an issue on this. The PDFClass makes an http request locally on the server and when doing that the DNS can return the external ip of the server, like 80.63.55.120 instead of the local internal IP, like 10.0.0.1. So - to avoid this behavior and the errors this will give it is needed to add a record to the servers hosts file: 127.0.0.1 www.some.dk. Make sure this is done also when going live on production servers and remember all hosts/domains that can be used to create the PDF.


 


//NP

 
Reply
Okay I tried this



protected void Page_Load(object sender, EventArgs e)

{

            String hash = Base.Request("invoice");

            if (hash.Length == 32)//Rock this joint

            {

                    Response.Write(ViewInvoice(f));

                    Dynamicweb.PDFClass pdf = new PDFClass(Request.Url.ToString());

                    pdf.HttpStream();

                    pdf.Dispose();                 

             }

}



On my localhost I get this error:

DLL'en 'ABCpdfCE6.dll' kunne ikke indlæses: Det angivne modul blev ikke fundet. (Undtagelse fra HRESULT: 0x8007007E)


On the online solution I got this message




Unable to render HTML. Page load timed out. Unable to load page.




url: http://rentamini.net.dynamicweb.dk/GetInvoice.aspx?invoice=5B78583B25BB5E0C17412E0C18177954





Seems like theres a problem with the
'ABCpdfCE6.dll'.

Or is it something else?



- Sune
 
Nicolai Høeg Pedersen
Reply

Hi Sune


 


It seems like you are making a loop. In GetInvoice.aspx you make a call to GetInvoice.aspx to print in PDF which will do the same again and you have a loop...


 


You need to make a .aspx page that prints the HTML you want as PDF. Then in another .aspx file make the call to that file using PDFClass - and you should be all set.

 
Reply


Okay, partial success



now I get this errormessage:



Any idea what can be done about it?




Server Error in '/' Application.


Unable to add image.


Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.



Exception Details: WebSupergoo.ABCpdf6.Internal.PDFException: Unable to add image.



Source Error:









An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.



Stack Trace:










[PDFException: Unable to add image.]
WebSupergoo.ABCpdf6.Doc.AddUrl(String url, Boolean paged, Int32 width, Boolean disableCache) +208
WebSupergoo.ABCpdf6.Doc.AddImageUrl(String url, Boolean paged, Int32 width, Boolean disableCache) +58
WebSupergoo.ABCpdf6.Doc.AddImageUrl(String url) +58
Dynamicweb.PDFClass..ctor(String strUrl) +172

[Exception: PDFClass constructor exception]
Dynamicweb.PDFClass..ctor(String strUrl) +520
CustomModules.BookingModule.GetInvoicePDF.Page_Load(Object sender, EventArgs e) +116
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061






Version Information: Microsoft .NET Framework Version:2.0.50727.832; ASP.NET Version:2.0.50727.832
 
Nicolai Høeg Pedersen
Reply

If I try http://rentamini.net.dynamicweb.dk/GetInvoicePDF.aspx it gives an error - that page should be able to print the invoice in html - then you can have the PDFClass eat it... Or am i missig something?

 
Reply
The url needs an invoice id like this:



http://rentamini.net.dynamicweb.dk/GetInvoicePDF.aspx?invoice=C7F64044BDEA24B81008D4B3A1C09274



the GetInvoicePDF page looks ike this:



        protected void Page_Load(object sender, EventArgs e)

        {

            String hash = Base.Request("invoice");

            if (hash.Length == 32)

            {

                Dynamicweb.PDFClass pdf = new PDFClass("GetInvoice.aspx?invoice=" + hash);

                pdf.HttpStream();

                pdf.Dispose();                            

            }

        }




It is supposed to generate a pdf from the GetInvoice.aspx page an send it to the http stream.



Am I still doing it wrong?
 
Nicolai Høeg Pedersen
Reply

Propably you need the entire URL in the call:


 


Dynamicweb.PDFClass pdf = new PDFClass("http://rentamini.net.dynamicweb.dk/GetInvoice.aspx?invoice=" + hash);

 
Reply
Great!

That did the trick. thanks a lot
 
Reply
Do you by chance know if there is difficulties loading images into the pdf?



if you view this URL: http://rentamini.net.dynamicweb.dk/GetInvoice.aspx?invoice=B4533E2140A5456EFE748E17E2BC48C9



and the pdf converted same:

http://rentamini.net.dynamicweb.dk/GetInvoicePDF.aspx?invoice=B4533E2140A5456EFE748E17E2BC48C9



The image doesnt appear in the pdf.



Is it possible to get imagelinks in the html embedded in the pdf?
 
Nicolai Høeg Pedersen
Reply

It can take images - its the same function that Dynamicweb uses for PDF printer friendly feature.


 


I think there can be 2 things wrong. Your HTML in Getinvoice is not good - fix that. More important; change the image URL to a relative URL src=/Files/asm/ads.jpg instead.

 
Reply
Okay works like a charm now



thank you

 

You must be logged in to post in the forum