2

I have a C# code behind file that gets dynamically generated HTML from a server and converts it to a PDF. In the HTML there are <link rel="stylesheet"> elements that reference external stylesheets as well as many <img> tags.

All of these files, images and stylesheets, come from www.example.com, and I'm trying to make all elements come from static.example.com with the same url instead. I've used the obvious stringName.Replace("www.example.com","static.example.com") to replace the elements in the HTML, but is there any way to do so for the references in the CSS file?

FOR EXAMPLE:
background-image:url('www.example.com/bg.png');
needs to become
background-image:url('static.example.com/bg.png');
in the external file.

Any ideas on approach? Creativity is welcome; I'm up for anything!

Thanks :)

1
  • the /path/to/file.png url is the same on each server, btw. Commented Mar 9, 2011 at 18:28

4 Answers 4

3

Since you say "code behind file"... are you doing this in ASP.NET?

You could use String.Replace functions in the HTML code to change <link href="www.example.com/cssfile.css"> to <link href="fixcss.ashx?file=www.example.com/cssfile.css">. Then write a fixcss.ashx to retrieve the CSS file and perform the string replacements on it.

Sign up to request clarification or add additional context in comments.

2 Comments

I am, and that sounds like a great solution. Thanks!
+1 to this. Also, be sure you cache the result (either using the system cache, or better still write it to disk or database) so that you're not doing the string parsing on every request if you don't have to.
2

You may use filestream to get the file and then replace by the code you have used.Other approaches are open

You may find this like helpful

CSS Parser

Reading and Parsing a CSS file

3 Comments

I'm a longtime PHP programmer, and C# is pretty new to me. So the suggestion is to actually edit the file? Or would opening it in the filestream let the program treat the file as if it were "edited." I pass the HTML to Syncfusion's PDF library to convert it. The MSHTML engine inside renders the file and draws a bitmap. Would reading the file and making the replacements set a situational understanding with the program that the file has these changes? I'm thinking I might have to write an "override server" of sorts that, for the purposes of the renderer, has different file contents.
file stream will open the file as you have to give it the paths first please tell me that you are working in ASP.Net of Winforms?
I think you should have a look on the links they parse the css file and you can do what ever you wnat
1

Aha, so here's an idea I had. This might be inefficient, but here's what I think I could do:

Change each <link rel="stylesheet"> into a <style> ... </style> tag and read the file contents, replacing the external stylesheet with an embedded one.

This might be inefficient, thoughts?

Thank you!

Comments

1

Just for some variety (and for other folks who might be in a different environment), if you have access to the to it, you can (and should) use the URL Rewrite module. It can not only redirect (like .htaccess in PHP apps would) but also rewrite the URL in files that are served (so for example, your CSS file).

1 Comment

Thanks, and if I weren't in a strict IIS - .NET environment that would be a great solution. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.