xyzio

Posts Tagged ‘Mono

Generating PDFs using iTextSharp on Mono

leave a comment »

I was looking for a .NET pdf generator so that I could add online bingocard generation to my side project, BingoWords.  The code and website are built using .NET and I run the site under Mono on a VPS at Linode.  So, it was important that the PDF generator run under Mono without issues.  Being cheap and full featured were two other desired features.  After some research, I found that there were two major players that satisfied this criteria,  PDFSharp and  iTextSharp.  Both are open source, free, and are fully featured.

Although some posts on the web indicated that PDFSharp would run under Mono, it was a no-go from  the beginning.  It failed the mono compatibility tool – Mono Migration Analyzer or M0MA almost right away.  I still decided to try out PDFSharp with Mono after reading a few posts stating that it may work if my code avoided the non-Mono compatible portions of the code but that was a total failure.  

So, I had no choice but to use iTextSharp.  iTextSharp passed under MoMA and it worked great on my Linode VPS under Mono.  With iTextSharp, I can generate PDFs that comply with the PDF standard and are viewable on every major platform.  So, if you are planning to generate PDFs under Mono, I would highly recommend iTextSharp.  Or to get a better sense of the features, try out my free Online Bingo Card Generator!

 I noticed that weren’t many code samples available while doing my research and coding the Bingo Card Generator.  So, here are some samples from my code for those that are just getting started:

Creating a new PDF document:

MemoryStream ms = new MemoryStream();
Document document = new Document(PageSize.LETTER);
PdfWriter writer = PdfWriter.GetInstance(document, ms);
document.Open();

Creating a BaseFont.  A BaseFont contains the parameters of the font that is written to the PDF document:

BaseFont bf = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

Writing center-aligned text to 400pts from the left and 400pts from the top:

cb.BeginText();
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, “Title Text”, (int)(Math.Abs(400, 400, 0);
cb.EndText();

Getting the width of a string in points:

titleWidth = (int)cb.GetEffectiveStringWidth(“Hello World!”, false);
Setting the font size of a BaseFont:
cb.SetFontAndSize(bf, titleFontSize);

Draw a line from the top left to the bottom left of the document:

cb.MoveTo(document.Left, document.Top);
cb.LineTo(document.Left, document.Bottom);
cb.Stroke();

Editing the document’s properties:

document.AddTitle(“Designed by BingoWords.com”);
document.AddAuthor(“Author goes here!”);
document.AddSubject(“Subject Goes Here!”);
document.AddKeywords(“Keyword1, keyword2, …”);

Closing your document:

document.Close();

Put this after the “document.Close()” to return your document with the name “file.pdf” when a user clicks on a link:

Response.ContentType = “application/pdf”;
Response.AddHeader(“content-disposition”, “attachment;filename=file.pdf”);
Response.Buffer = true;
Response.Clear();
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
Response.OutputStream.Flush();
Response.End();

I hope this comes in handy.  Feel free to contact me if you have any questions or comments.

Advertisements

Written by M Kapoor

June 20, 2010 at 5:52 am

Free Bingo Card and Wordsearch Designer

leave a comment »

If you are looking for a free Bingo Card or Wordsearch Designer then look no more – BingoWords Puzzle Designer is now free for everybody!  Click here to go to the website!

Features:

  • Create arbitrary sized Wordsearch puzzles
  • Create Bingo Cards up to 5×5 squares in size
  • Save your wordlists for future use and to share with co-workers
  • Print an unlimited number of Bingo Cards and Word Search Puzzles
  • Print up to 8 Bingo Cards per page
  • Quick access to common word lists using the built in Word Wizard

For more details, read on.

Why is it Free?

Originally, I was planning to sell Bingo Words Puzzle Designer but then I realized that I’d rather spend my free time working on new code instead of on marketing and all the hassle that comes with running a business.  Besides, putting together something like this only took me a couple weeks of part-time work and it annoys me that some people are charging over $30/copy for something that is this simple to make.

My hope is that it will be useful to someone and that they will pay it forward some day.

About

BingoWords Puzzle Designer is a project I worked on in order to teach myself C#/ASP.NET.  The application is made with C# and Winforms.  The website is build using ASP.NET and is hosted using Mono on a  virtual server at Linode.  I was able to learn every aspect of creating an app with this project – from drawing on the screen to printing and to saving files to generating serial numbers.

Feel free to contact me with questions or suggestions for improvement!

Written by M Kapoor

February 19, 2010 at 4:06 am

CrystalTech Web Hosting Review

leave a comment »

I was looking for ASP.NET hosting for the project I built to learn C#/ASP.NET.  CrystalTech isn’t the cheapest host or gives the most features for your dollar but I decided to sign up because it is the host used and recommended by Jeff Atwood.

Background

I signed up for the .NET Value plan at $7.95/month.  I got the following for my $7.95/month:

  • Hosting for 1 site only
  • 500 MB disk space
  • 50 GB bandwidth
  • 2 GB mail disk space
  • 10 mail accounts
  • 100 MB MySQL database

I paid for 3 months in advance so I didn’t have to pay the setup fee.  I was a member from July 2008 through February 2009.

Sign-up

Signing up was easy.  I filled out the online form, entered my credit card number, and they started on setting up my account.    I finished applying at 8:18PM and had my account information at 8:58PM on the same day.

Control Panel

CrystalTech’s control panel is the worst I have used.  They make you log in with a hard to remember customer number instead of a easy to remember user ID.  Instead of giving you a overview of your site, the main control panel page is a massive advertisement for their services.  I am logged in now and I see ads for Paypal and virtual servers – and this is for a service that I’ve paid for!  Even worse, the control panel has poor support for non-IE browsers, I cannot access any of the settings in the control panel with my daily browser – Opera.

Hosting

Visual Studio has a great GUI for transferring websites and I was easily able to use it to upload my files after I managed to set up a FTP account.  Their uptime isn’t too great, I get maintenance notifications 1-2 times a month alerting me that my site will be down for a couple hours.  Their last downtime notification was about 2 weeks ago, compare this with my linode virtual server that has been up for 46 days!

Account Cancellation

Unlike all the other hosting companies I have used, CrystalTech makes you call in to cancel your account.  Canceling online is not allowed.

Final Thoughts

I was very disappointed with CrystalTech’s hosting experience.  Their constant downtime, poorly designed control panel, and ads are a pain.  In addition, they’ve graciously decided to spam me with their monthly newsletters.  Their poor service is apparent when a little while after I signed up, they lost all the data that Jeff Atwood had stored on their servers.

Since then, I’ve moved my site to Linode and set it up to serve ASP.NET pages using Mono.  For about $12 more (1.5x more), I get a shell account, dedicated memory, 4x more bandwidth, 32x more disk space, and the ability to host unlimited websites.  I would not recommend CrystalTech as a host.

Written by M Kapoor

February 18, 2010 at 5:34 am

Getting Mono Working at Linode on Ubuntu 9.10

leave a comment »

I’m looking into moving one of my ASP.NET projects to Mono and had trouble getting Mono working on Ubuntu 9.10. Turns out the steps to set it up were simple, but they took some digging around so here they are:

Step 1:

Install Apache2:
apt-get install apache2 apache2.2-common apache2-mpm-prefork apache2-utils libexpat1 ssl-cert

Step 2:

Enable the Universe repositories. To do this, uncomment the universe repositories in /etc/apt/sources.list so it looks like this:

## main; restricted repositories
deb http://us.archive.ubuntu.com/ubuntu/ karmic main restricted
deb-src http://us.archive.ubuntu.com/ubuntu/ karmic main restricted

deb http://security.ubuntu.com/ubuntu karmic-security main restricted
deb-src http://security.ubuntu.com/ubuntu karmic-security main restricted

## universe repositories
deb http://us.archive.ubuntu.com/ubuntu/ karmic universe
deb-src http://us.archive.ubuntu.com/ubuntu/ karmic universe

deb http://us.archive.ubuntu.com/ubuntu/ karmic-updates universe
deb-src http://us.archive.ubuntu.com/ubuntu/ karmic-updates universe

deb http://security.ubuntu.com/ubuntu karmic-security universe
deb-src http://security.ubuntu.com/ubuntu karmic-security universe

Step 3:

Run ‘apt-get update’ to get the latest sources.

Step 4:

Finally, follow the steps listed in the Ubuntu ModMono guide:

Install the mod_mono packages: libapache2-mod-mono mono-apache-server2-
apt-get install libapache2-mod-mono mono-apache-server2

Enable the Apache module:
a2enmod mod_mono_auto

Restart Apache:
apache2ctl graceful

Step 5:
Place a .aspx page where Apache can see it, for example at ‘/var/www/’

Written by M Kapoor

January 2, 2010 at 10:03 pm

Posted in ASP.NET, hosting, Linode, Mono

Tagged with , , ,