pdfHummusTest1.cpp 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* Found that previous library was complied with the MSVC compiler and not compatible with GNU toolchain. Tried to use Msys with PDFHummus Cmake build tools and it was hard-coded
  2. * to MSVC. So building for GNU compiler will take some modification of Cmake.
  3. * 4.16.20 - after getting an image onto the pdf, we realized there was an error that was not getting logged/presented to us.
  4. * we came upon this error, by trying to run the pdfHummusTest1.exe and the pdf file was not being created (because it was still
  5. * open -> Windows locking files regardless of where they are open). This is the reason why we wanted to figure out if PDFHummus
  6. * had an error logging capability and then we had to try and figure out how to use it... to be continued for next time.
  7. * 19 April 2020
  8. * Gal's documentation for using the logging capabilitites was old, the LogConfiguration function takes 3 parameters.
  9. * Gal does not actually handle or log errors.
  10. */
  11. #include <stdlib.h>
  12. #include <iostream>
  13. #include "PDFWriter.h"
  14. #include "PDFPage.h"
  15. #include "PageContentContext.h"
  16. #include "PDFFormXObject.h"
  17. #include "Trace.h"
  18. int main(){
  19. printf("if running on Windows check here for file(s): %%AppData%%\\Local\\VirtualStore\n");
  20. PDFWriter pdfWriter;
  21. // PREPARE PDF DOCUMENT
  22. // ********************
  23. // get a PDFWriter object to interact with. set the PDF version, set log file
  24. EStatusCode e = pdfWriter.StartPDF("c:\\myFile.pdf",ePDFVersion13,LogConfiguration(true,true,".\\logMe"));
  25. if (e){
  26. /* 25APR20 - look through codebase and found these three reasons for nonrecoverable failure:
  27. * - user requested encryption, document doesn't support it
  28. * - it is unlikely, but for some reason GK wanted to call OutputFile::CloseFile(), it failed
  29. * - operating system was unsuccessful at giving a lock for the filename *
  30. *
  31. * * Windows 'gives' a lock, but doesn't protect others from writing to this filename
  32. */
  33. printf("Terminating (c:\\myFile.pdf): unsuccessful lock or couldn't close existing or requested encryption but not supported\n");
  34. TRACE_LOG("Terminating (c:\\myFile.pdf): unsuccessful lock or couldn't close existing or requested encryption but not supported");
  35. exit(1);
  36. }
  37. TRACE_LOG("Notified OS we might use c:\\myFile.pdf");
  38. PDFPage* pdfPage = new PDFPage();
  39. // set dimensions for an A4 sized page
  40. pdfPage->SetMediaBox(PDFRectangle(0,0,595,842));
  41. PageContentContext* pageContentContext = pdfWriter.StartPageContentContext(pdfPage);
  42. // include font file
  43. PDFUsedFont* arialTTF = pdfWriter.GetFontForFile("C:\\Windows\\Fonts\\arial.ttf");
  44. // include image file
  45. // ADD CONTENT
  46. // ********************
  47. // set font color to black
  48. pageContentContext->k(0,0,0,1);
  49. // set text object
  50. pageContentContext->BT();
  51. // set font
  52. pageContentContext->Tf(arialTTF,1);
  53. // set position of text
  54. pageContentContext->Tm(20,0,0,20,40,822);
  55. // insert text into PDF
  56. pageContentContext->Tj("Text placed and scaled with Tm");
  57. // end of text
  58. pageContentContext->ET();
  59. pdfWriter.EndPageContentContext(pageContentContext);
  60. pdfWriter.WritePageAndRelease(pdfPage);
  61. // ****************Shutdown Usage************
  62. // ******************************************
  63. // ******************************************
  64. pdfWriter.Shutdown("c:\\myFile.pdf.shutdown");
  65. PDFWriter pdfWriterB;
  66. pdfWriterB.ContinuePDF("c:\\myFile.pdf","myFile.pdf.shutdown","",LogConfiguration(true,true,".\\logMe"));
  67. PDFPage* pdfPageB = new PDFPage();
  68. // set dimensions for an A4 sized page
  69. pdfPageB->SetMediaBox(PDFRectangle(0,0,595,842));
  70. PageContentContext* pageContentContextB = pdfWriterB.StartPageContentContext(pdfPageB);
  71. PDFFormXObject* image = pdfWriterB.CreateFormXObjectFromJPGFile("C:\\Users\\bad-p\\Desktop\\Work Folder\\VS CODE\\SanAntonioPass.jpg");
  72. if (!image){
  73. TRACE_LOG("Image file not found, creating PDF without image.");
  74. printf("The image \"C:\\Users\\bad-p\\Desktop\\Work Folder\\VS CODE\\SanAntonioPass.jpg\" was not found.");
  75. }
  76. pageContentContextB->q();
  77. pageContentContextB->cm(0.4,0,0,0.4,57.5,241);
  78. pageContentContextB->Do(pdfPageB->GetResourcesDictionary().AddFormXObjectMapping(image->GetObjectID()));
  79. pageContentContextB->Q();
  80. delete image;
  81. // END PAGE, WRITE PDF.
  82. // ********************
  83. pdfWriterB.EndPageContentContext(pageContentContextB);
  84. EStatusCode f = pdfWriterB.WritePageAndRelease(pdfPageB);
  85. if (f){
  86. printf ("something interesting");
  87. }
  88. pdfWriterB.EndPDF();
  89. }