iTextSharp: problems with relative paths during conversion of html to pdf (HTMLWorker)

View: New views
3 Messages — Rating Filter:   Alert me  

iTextSharp: problems with relative paths during conversion of html to pdf (HTMLWorker)

by Tommy Sanddal Poulsen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

Hi,

I am looking into using iTextSharp in the CMS system I use, creating a pdf of part of the content.

My problem is, that HTMLWorker.ParseToList seems to choke on relative image source-paths during conversion of html to pdf (resulting in an empty pdf-document).

Is this a known issue, and does anyone have a nice solution to it?

 

Regards

>Tommy

 


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
itextsharp-questions mailing list
itextsharp-questions@...
https://lists.sourceforge.net/lists/listinfo/itextsharp-questions

Re: iTextSharp: problems with relative paths during conversion of html to pdf (HTMLWorker)

by Jay80 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I had the same problem.  iTextSharp is trying load the images using absolute paths.  So if you only provide it a relative, it will try to resolve it the best way it knows how by appending an absolute file system path to it.  The problem is that the path it appends usually doesn't contain the file you are looking for.  There are two solutions to the problem:

1. Provide a fully qualified URL to the image.  For Example: http://www.mysite.com/images/image.jpg
2. With .NET, you can use Server.MapPath("~/images/image.jpg").  This will get an absolute path to the image on the file system.

I handled this by creating a method that parsed all images from my HTML and added the appropriate domain information to image links based on the current application path.

Tommy Sanddal Poulsen wrote:
Hi,

I am looking into using iTextSharp in the CMS system I use, creating a pdf
of part of the content.

My problem is, that HTMLWorker.ParseToList seems to choke on relative image
source-paths during conversion of html to pdf (resulting in an empty
pdf-document).

Is this a known issue, and does anyone have a nice solution to it?

 

Regards

>Tommy

 


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
itextsharp-questions mailing list
itextsharp-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itextsharp-questions

Re: iTextSharp: problems with relative paths during conversion of html to pdf (HTMLWorker)

by Paulo Soares-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

There a number of interfaces to resolve the image location (not really well documented). See the following snippet of HTMLWorker:

if (interfaceProps != null) {
    IImageProvider ip = (IImageProvider)interfaceProps["img_provider"];
    if (ip != null)
        img = ip.GetImage(src, h, cprops, document);
    if (img == null) {
        Hashtable images = (Hashtable)interfaceProps["img_static"];
        if (images != null) {
            Image tim = (Image)images[src];
            if (tim != null)
                img = Image.GetInstance(tim);
        } else {
            if (!src.StartsWith("http")) { // relative src references only
                String baseurl = (String)interfaceProps["img_baseurl"];
                if (baseurl != null) {
                    src = baseurl + src;
                    img = Image.GetInstance(src);
                }
            }
        }
    }
}

Paulo

> -----Original Message-----
> From: Jay80 [mailto:stewart.jms@...]
> Sent: Thursday, September 03, 2009 4:13 PM
> To: itextsharp-questions@...
> Subject: Re: [itextsharp-questions] iTextSharp: problems with
> relative paths during conversion of html to pdf (HTMLWorker)
>
>
> I had the same problem.  iTextSharp is trying load the images
> using absolute
> paths.  So if you only provide it a relative, it will try to
> resolve it the
> best way it knows how by appending an absolute file system
> path to it.  The
> problem is that the path it appends usually doesn't contain
> the file you are
> looking for.  There are two solutions to the problem:
>
> 1. Provide a fully qualified URL to the image.  For Example:
> http://www.mysite.com/images/image.jpg
> 2. With .NET, you can use
> Server.MapPath("~/images/image.jpg").  This will
> get an absolute path to the image on the file system.
>
> I handled this by creating a method that parsed all images
> from my HTML and
> added the appropriate domain information to image links based
> on the current
> application path.
>
>
> Tommy Sanddal Poulsen wrote:
> >
> > Hi,
> >
> > I am looking into using iTextSharp in the CMS system I use,
> creating a pdf
> > of part of the content.
> >
> > My problem is, that HTMLWorker.ParseToList seems to choke
> on relative
> > image
> > source-paths during conversion of html to pdf (resulting in an empty
> > pdf-document).
> >
> > Is this a known issue, and does anyone have a nice solution to it?
> >
> >  
> >
> > Regards
> >
> >>Tommy

Aviso Legal:
Esta mensagem é destinada exclusivamente ao destinatário. Pode conter informação confidencial ou legalmente protegida. A incorrecta transmissão desta mensagem não significa a perca de confidencialidade. Se esta mensagem for recebida por engano, por favor envie-a de volta para o remetente e apague-a do seu sistema de imediato. É proibido a qualquer pessoa que não o destinatário de usar, revelar ou distribuir qualquer parte desta mensagem.

Disclaimer:
This message is destined exclusively to the intended receiver. It may contain confidential or legally protected information. The incorrect transmission of this message does not mean the loss of its confidentiality. If this message is received by mistake, please send it back to the sender and delete it from your system immediately. It is forbidden to any person who is not the intended receiver to use, distribute or copy any part of this message.



------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
itextsharp-questions mailing list
itextsharp-questions@...
https://lists.sourceforge.net/lists/listinfo/itextsharp-questions