wxPdfDocument - Document is Empty Topic is solved

Talk here about issues with one of the components hosted at wxCode, or suggest features for it.
Post Reply
wxProgrammer
Experienced Solver
Experienced Solver
Posts: 96
Joined: Thu Apr 17, 2014 10:10 am

wxPdfDocument - Document is Empty

Post by wxProgrammer »

Code: Select all

wxPdfDocument pdf;
		
		pdf.AddPage();
		pdf.SetFont(wxT("Helvetica"), wxT(""), 12);
		pdf.SetMargins(0, 20, 20);
		pdf.SetTitle((*analysis)[ANALYSIS_FIELDS_NAME]);
		pdf.SetAuthor(wxT("La Clinica del PC"));
		pdf.Open();
		pdf.SetY(297/2);
		pdf.Ln();
		
		for (int i(0); i < 2; ++i)
		{
			pdf.SetY(i * (pdf.GetHeight()/2) + 20);

			wxString HEAD = wxT("<table><tr><td><img src='images/pdf_logo.png' /></td><td>Copia ");
			HEAD += (!i) ? wxT("Analisi") : wxT("Cliente");
			HEAD += wxT("</td></tr></table>");
			pdf.WriteXml(HEAD);
			pdf.WriteXml(wxT("ciao"));
		}
				
		pdf.SaveAsFile(dlg.GetPath());
Why the document is empty? It's saved but the page is white, empty.
I'm Italian but we can speak C++ :)
utelle
Moderator
Moderator
Posts: 1125
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: wxPdfDocument - Document is Empty

Post by utelle »

wxProgrammer wrote:

Code: Select all

wxPdfDocument pdf;
		
		pdf.AddPage();
		pdf.SetFont(wxT("Helvetica"), wxT(""), 12);
		pdf.SetMargins(0, 20, 20);
		pdf.SetTitle((*analysis)[ANALYSIS_FIELDS_NAME]);
		pdf.SetAuthor(wxT("La Clinica del PC"));
		pdf.Open();
If you make an explicit call to method Open, then it must be the first thing you do. Please refer to the wxPdfDocument API documentation of method Open:
This method begins the generation of the PDF document.

It is not necessary to call it explicitly because AddPage() does it automatically. Note: no page is created by this method
Method AddPage already called method Open implicitly, therefore calling method Open again messes up the internal state of wxPdfDocument. Just remove the call to method Open, and the generated document should no longer be empty.

Besides that keep in mind that method SetTitle sets the meta data field of the generated PDF document, that is, no visible output is produced by this method.
wxProgrammer wrote:

Code: Select all

		pdf.SetY(297/2);
		pdf.Ln();
		
		for (int i(0); i < 2; ++i)
		{
			pdf.SetY(i * (pdf.GetHeight()/2) + 20);

			wxString HEAD = wxT("<table><tr><td><img src='images/pdf_logo.png' /></td><td>Copia ");
			HEAD += (!i) ? wxT("Analisi") : wxT("Cliente");
			HEAD += wxT("</td></tr></table>");
			pdf.WriteXml(HEAD);
			pdf.WriteXml(wxT("ciao"));
		}
				
		pdf.SaveAsFile(dlg.GetPath());
Why the document is empty? It's saved but the page is white, empty.
Your first call to method AddPage creates a new page. However, not output is written to this page, before the (at this point invalid) call to method Open. Therefore the page is blank. Since method Open resets the internal state and no call to method AddPage follows, the following output methods do not create visible content.

I would advise to inspect the numerous samples coming with wxPdfDocument to get a feeling how wxPdfDocument works.

Regards,

Ulrich
wxProgrammer
Experienced Solver
Experienced Solver
Posts: 96
Joined: Thu Apr 17, 2014 10:10 am

Re: wxPdfDocument - Document is Empty

Post by wxProgrammer »

Hi, thank you for the answer :)
Now work: if I write "pdf.WriteXml("hello");, work but this don't work, no table and no image/string is printed...

Code: Select all

for (int i(0); i < 2; ++i)
		{
			pdf.SetY(i * (pdf.GetPageHeight()/2) + 20);

			wxString HEAD = wxT("<table border=\"1\"><tr><td><img src=\"images\\pdf_logo.png\" /></td><td>Copia ");
			HEAD += (!i) ? wxT("Analisi") : wxT("Cliente");
			HEAD += wxT("</td></tr></table>");
			pdf.WriteXml(HEAD);
		}
EDIT: I have changed code; now don't work neither WriteXml(wxT("Text"));

Code: Select all

wxPdfDocument pdfTemplateU(wxPORTRAIT, wxT("mm"), wxPAPER_A5);
		
		pdfTemplateU.BeginTemplate();
		pdfTemplateU.AddPage();
		pdfTemplateU.SetFont(wxT("Helvetica"), wxT(""), 12);
		pdfTemplateU.SetMargins(20, 20, 20);
		
		pdfTemplateU.WriteXml(GetHeader(wxT("Analisi")));
		pdfTemplateU.WriteXml(wxT("LOL1"));

		int firstID = pdfTemplateU.EndTemplate();


		
		wxPdfDocument pdfTemplateD(wxPORTRAIT, wxT("mm"), wxPAPER_A5);
		
		pdfTemplateD.BeginTemplate();
		pdfTemplateD.AddPage();
		pdfTemplateD.SetFont(wxT("Helvetica"), wxT(""), 12);
		pdfTemplateD.SetMargins(20, 20, 20);

		pdfTemplateD.WriteXml(GetHeader(wxT("Cliente")));
		pdfTemplateD.WriteXml(wxT("LOL2"));

		int secondID = pdfTemplateD.EndTemplate();
		


		wxPdfDocument pdf;
		pdf.AddPage();
		pdf.SetTitle((*analysis)[ANALYSIS_FIELDS_NAME]);
		pdf.SetAuthor(wxT("La Clinica del PC"));
		pdf.UseTemplate(firstID);
		pdf.Ln();
		pdf.UseTemplate(secondID);

		pdf.SaveAsFile(dlg.GetPath());
I'm Italian but we can speak C++ :)
utelle
Moderator
Moderator
Posts: 1125
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: wxPdfDocument - Document is Empty

Post by utelle »

wxProgrammer wrote:Hi, thank you for the answer :)
Now work: if I write "pdf.WriteXml("hello");, work but this don't work, no table and no image/string is printed...

Code: Select all

for (int i(0); i < 2; ++i)
		{
			pdf.SetY(i * (pdf.GetPageHeight()/2) + 20);

			wxString HEAD = wxT("<table border=\"1\"><tr><td><img src=\"images\\pdf_logo.png\" /></td><td>Copia ");
			HEAD += (!i) ? wxT("Analisi") : wxT("Cliente");
			HEAD += wxT("</td></tr></table>");
			pdf.WriteXml(HEAD);
		}
Support for XML / HTML in wxPdfDocument is limited. Especially for tables you have to obey the information given in the wxPdfDocument documentation, that is, you have to explicitly specify the widths of the columns using special tags colgroup and col, and you have to mark the table body with the tbody tag.

For an image it is usually recommended to add at least the attribute height in pixels to specify the dimension of the height of the space the image should occupy, although it is not absolutely necessary. Additionally you should verify that the image file can be located under the given path.

Regards,

Ulrich
wxProgrammer
Experienced Solver
Experienced Solver
Posts: 96
Joined: Thu Apr 17, 2014 10:10 am

Re: wxPdfDocument - Document is Empty

Post by wxProgrammer »

Oh, then I use simply "Image" and "Write", seeing i have to write only text with an image :)
Thank you for your help :)
I'm Italian but we can speak C++ :)
Post Reply