wxPdfDocument - Save Pdf 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 - Save Pdf

Post by wxProgrammer »

Hi! This function is called correctly but pdf isn't saved..

void OnExport(wxCommandEvent& WXUNUSED(event))
{
wxFileDialog dlg(this, wxT("ESPORTA PDF"), wxEmptyString, wxEmptyString, wxT("Documento PDF (*.pdf)|(*.pdf)"), wxSAVE | wxOVERWRITE_PROMPT);
if (dlg.ShowModal() != wxOK) return;

wxPdfDocument pdf;

pdf.AddPage();
pdf.SetFont(wxT("Helvetica"), wxT(""), 12);
pdf.SetMargins(20, 20, 20);

for (int i(0); i < 2; ++i)
{
wxString HEAD = wxT("<b>MyHTML</b>");
pdf.WriteXml(HEAD);
}



wxString working_dir = wxGetWorkingDirectory();
wxSetWorkingDirectory(dlg.GetPath());
pdf.SaveAsFile(dlg.GetFilename() + wxT(".pdf"));

wxFileType* fileType = wxTheMimeTypesManager -> GetFileTypeFromExtension(wxT("pdf"));

if (fileType)
{
wxString cmd = fileType -> GetOpenCommand(dlg.GetFilename() + wxT(".pdf"));
if (!cmd.IsEmpty()) wxExecute(cmd);
delete fileType;
}
wxSetWorkingDirectory(working_dir);

fileType = NULL;
}
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 - Save Pdf

Post by utelle »

wxProgrammer wrote:Hi! This function is called correctly but pdf isn't saved..

void OnExport(wxCommandEvent& WXUNUSED(event))
{
wxFileDialog dlg(this, wxT("ESPORTA PDF"), wxEmptyString, wxEmptyString, wxT("Documento PDF (*.pdf)|(*.pdf)"), wxSAVE | wxOVERWRITE_PROMPT);
You should replace wxSAVE and wxOVERWRITE_PROMPT by wxFD_SAVE and wxFD_OVERWRITE_PROMPT respectively, since the constants you used are outdated and will only work if wxWidgets was compiled with version 2.6 compatibility.
wxProgrammer wrote: if (dlg.ShowModal() != wxOK) return;

wxPdfDocument pdf;

pdf.AddPage();
pdf.SetFont(wxT("Helvetica"), wxT(""), 12);
pdf.SetMargins(20, 20, 20);

for (int i(0); i < 2; ++i)
{
wxString HEAD = wxT("<b>MyHTML</b>");
pdf.WriteXml(HEAD);
}

wxString working_dir = wxGetWorkingDirectory();
wxSetWorkingDirectory(dlg.GetPath());
dlg.Path returns the full path (directory and filename) of the selected file. The argument for wxSetWorkingDirectory should be a directory only. I'm not sure whether the working directory is set as expected if you pass a filename to this function.
wxProgrammer wrote: pdf.SaveAsFile(dlg.GetFilename() + wxT(".pdf"));
According to the documentation dlg.GetFilename returns the default filename (which you set as an empty string). You should use dlg.GetPath to get the filename selected in the file selection dialog.
wxProgrammer wrote: wxFileType* fileType = wxTheMimeTypesManager -> GetFileTypeFromExtension(wxT("pdf"));

if (fileType)
{
wxString cmd = fileType -> GetOpenCommand(dlg.GetFilename() + wxT(".pdf"));
if (!cmd.IsEmpty()) wxExecute(cmd);
delete fileType;
}
wxSetWorkingDirectory(working_dir);

fileType = NULL;
}
In principle the code should work, provided that the filename is valid. What behaviour do you get? Do you get warnings or error messages? Is the PDF viewer (Adobe Reader or similar) started? If yes, what is displayed?

You could / should use wxMessageBox to display the filename used in method SaveAsFile and the command passed to wxExecute. Maybe this gives you clou where to look for the generated PDF file.

Regards,

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

Re: wxPdfDocument - Save Pdf

Post by wxProgrammer »

wxFileDialog dlg(this, wxT("ESPORTA PDF"), wxEmptyString, wxEmptyString, wxT("Documento PDF (*.pdf)|*.pdf"), wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
if (dlg.ShowModal() == wxID_CANCEL) return;

Now work

EDIT: I have read now your post, now I change with GetPath() thank you :)
I'm Italian but we can speak C++ :)
wxProgrammer
Experienced Solver
Experienced Solver
Posts: 96
Joined: Thu Apr 17, 2014 10:10 am

Re: wxPdfDocument - Save Pdf

Post by wxProgrammer »

Work, and the PDF Reader is shown, thank you :)
I'm Italian but we can speak C++ :)
Post Reply