Create PDF with special characters

If you like to create PDF documents in Xojo, you may just try the built-in PDFDocument class. But then you may run into a couple of issues when you need to print unicode characters. The PDFDocument class only does ANSI encoding.

Your document may need to print some characters in different charsets like Japanese, Chinese, Korean or just eastern Europe characters. This could be a report with customer names, where you can’t be sure they only use ASCII characters.

Luckily you can currently get DynaPDF Starter with OmegaBundle or buy it directly from Monkeybread Software. DynaPDF can load fonts from the system and get fully unicode support.

Using PDFDocument class

We go and draw some text into a new PDF Document. Sadly all the texts get converted and we get a lot of question marks.

var g as Graphics
	var f as FolderItem
	
	// try Xojo and make a new PDF
	Var doc As New PDFDocument
	
	// always a good idea to embed fonts
	doc.EmbeddedFonts = True
	
	// and get graphics for drawing to the page
	g = doc.Graphics
	f = SpecialFolder.Desktop.Child("test2.pdf")
	
	// we pick a font, which supports all the characters
	g.FontName = "Arial Unicode MS"
	g.FontSize = 20
	
	g.DrawText "Xojo " + XojoVersionString, 50, 100
	
	// japanese
	g.DrawText "こんにちは、世界", 50, 200
	
	// chinese
	g.DrawText "你好,世界", 50, 300
	
	// polish
	g.DrawText "Witaj świecie", 50, 400
	
	// ukrainian
	g.DrawText "Привіт, світ", 50, 500
	
	doc.Save f
	
	// show file
	f.Launch

Download: test-pdf2.pdf, 15.2 MB.

Using DynaPDFMBS class

Now check very similar code with our MBS Xojo DynaPDF Plugin. We use our PageGraphics to integrate our plugin to graphics class. This way you can code using the methods and properties you know.

You either use CreateNewPDF in DynapdfMBS class with a folderitem to create the PDF file there. Or you pass nil to make it an in-memory PDF. Then you can use GetBuffer method after calling CloseFile to pick up the PDF and e.g. send it via email or store in a database.

// now try with DynaPDF
	Var pdf As New DynapdfMBS
	
	// For this example you can use a Starter or higher license.
	pdf.SetLicenseKey "Starter"
	
	// write to this file
	f = SpecialFolder.Desktop.Child("test1.pdf")
	Call pdf.CreateNewPDF f
	
	// new page
	Call pdf.Append
	
	// we use graphics class to draw, 
	   so we can use same commands as above for Xojo
	g = pdf.PageGraphics
	
	g.FontName = "Arial Unicode MS"
	g.FontSize = 20
	
	g.DrawText "DynaPDF " + DynaPDFMBS.GetDynaPDFVersion, 50, 100
	
	// japanese
	g.DrawText "こんにちは、世界", 50, 200
	
	// chinese
	g.DrawText "你好,世界", 50, 300
	
	// polish
	g.DrawText "Witaj świecie", 50, 400
	
	// ukrainian
	g.DrawText "Привіт, світ", 50, 500
	
	// close page and file
	Call pdf.EndPage
	Call pdf.CloseFile
	
	// show file
	f.Launch

Download: test-pdf1.pdf, 19.713 bytes

Finally we may point to the font embedding. With DynaPDF we default to embedding all non-standard PDF fonts. For the font Arial Unicode MS, DynaPDF does font subsetting. We only embed the characters used from the font into the file. This way the PDF from DynaPDF is small and below 20K. But PDFDocument class embeds the whole file, so you get a 15 MB file size for the PDF.

Please try DynaPDF and see if you can output better PDF documents. You can try all the features before ordering. You may see a dialog or a watermark due to the missing license. Let us know if you have questions.

1 Like