pdfHummusTest1.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #include <windows.h>
  19. #include <winnt.h>
  20. int main(){
  21. //
  22. // Go to: "%AppData%\..\Local\VirtualStore" - because Windows does not write to the C:\ drive
  23. printf("if running on Windows check here for file(s): %%AppData%%\\..\\Local\\VirtualStore\n");
  24. PDFWriter pdfWriter;
  25. // PREPARE PDF DOCUMENT
  26. // ********************
  27. // get a PDFWriter object to interact with. set the PDF version, set log file
  28. EStatusCode e = pdfWriter.StartPDF("c:\\myFile.pdf",ePDFVersion13,LogConfiguration(true,true,".\\logMe"));
  29. if (e){
  30. /* 25APR20 - look through codebase and found these three reasons for nonrecoverable failure:
  31. * - user requested encryption, document doesn't support it
  32. * - it is unlikely, but for some reason GK wanted to call OutputFile::CloseFile(), it failed
  33. * - operating system was unsuccessful at giving a lock for the filename *
  34. *
  35. * * Windows 'gives' a lock, but doesn't protect others from writing to this filename
  36. */
  37. printf("Terminating (c:\\myFile.pdf): unsuccessful lock or couldn't close existing or requested encryption but not supported\n");
  38. TRACE_LOG("Terminating (c:\\myFile.pdf): unsuccessful lock or couldn't close existing or requested encryption but not supported");
  39. exit(1);
  40. }
  41. TRACE_LOG("Notified OS we might use c:\\myFile.pdf");
  42. PDFPage* pdfPage = new PDFPage();
  43. // set dimensions for an A4 sized page
  44. pdfPage->SetMediaBox(PDFRectangle(0,0,595,842));
  45. PageContentContext* pageContentContext = pdfWriter.StartPageContentContext(pdfPage);
  46. // include font file
  47. PDFUsedFont* arialTTF = pdfWriter.GetFontForFile("C:\\Windows\\Fonts\\arial.ttf");
  48. // include image file
  49. // ADD CONTENT
  50. // ********************
  51. // set font color to black
  52. pageContentContext->k(0,0,0,1);
  53. // set text object
  54. pageContentContext->BT();
  55. // set font
  56. pageContentContext->Tf(arialTTF,1);
  57. // set position of text
  58. pageContentContext->Tm(20,0,0,20,40,822);
  59. // insert text into PDF
  60. pageContentContext->Tj("Text placed and scaled with Tm");
  61. // end of text
  62. pageContentContext->ET();
  63. pdfWriter.EndPageContentContext(pageContentContext);
  64. pdfWriter.WritePageAndRelease(pdfPage);
  65. // ****************Shutdown Usage************
  66. // ******************************************
  67. // ******************************************
  68. pdfWriter.Shutdown("c:\\myFile.pdf.shutdown");
  69. /*PDFWriter pdfWriterB;
  70. pdfWriterB.ContinuePDF("c:\\myFile.pdf","myFile.pdf.shutdown","",LogConfiguration(true,true,".\\logMe"));
  71. PDFPage* pdfPageB = new PDFPage();
  72. // set dimensions for an A4 sized page
  73. pdfPageB->SetMediaBox(PDFRectangle(0,0,595,842));
  74. PageContentContext* pageContentContextB = pdfWriterB.StartPageContentContext(pdfPageB);
  75. PDFFormXObject* image = pdfWriterB.CreateFormXObjectFromJPGFile("C:\\Users\\bad-p\\Desktop\\Work Folder\\VS CODE\\SanAntonioPass.jpg");
  76. if (!image){
  77. TRACE_LOG("Image file not found, creating PDF without image.");
  78. printf("The image \"C:\\Users\\bad-p\\Desktop\\Work Folder\\VS CODE\\SanAntonioPass.jpg\" was not found.");
  79. }
  80. pageContentContextB->q();
  81. pageContentContextB->cm(0.4,0,0,0.4,57.5,241);
  82. pageContentContextB->Do(pdfPageB->GetResourcesDictionary().AddFormXObjectMapping(image->GetObjectID()));
  83. pageContentContextB->Q();
  84. delete image;
  85. // END PAGE, WRITE PDF.
  86. // ********************
  87. pdfWriterB.EndPageContentContext(pageContentContextB);
  88. EStatusCode f = pdfWriterB.WritePageAndRelease(pdfPageB);
  89. if (f){
  90. printf ("something interesting");
  91. }
  92. pdfWriterB.EndPDF();*/
  93. PDFWriter pdfWriterB;
  94. pdfWriterB.ContinuePDF("C:\\Users\\bad-p\\AppData\\Local\\VirtualStore\\myFile.pdf","C:\\Users\\bad-p\\AppData\\Local\\VirtualStore\\myFile.pdf.shutdown");
  95. // Create a new page
  96. PDFPage* pdfPage2 = new PDFPage();
  97. pdfPage2->SetMediaBox(PDFRectangle(0,0,595,842));
  98. // Create a content context for the page
  99. PageContentContext* pageContentContext2 = pdfWriterB.StartPageContentContext(pdfPage2);
  100. PDFFormXObject* image = pdfWriterB.CreateFormXObjectFromJPGFile("C:\\Users\\bad-p\\Desktop\\Work Folder\\VS CODE\\SanAntonioPass.jpg");
  101. if (!image){
  102. TRACE_LOG("Image file not found, creating PDF without image.");
  103. printf("The image \"C:\\Users\\bad-p\\Desktop\\Work Folder\\VS CODE\\SanAntonioPass.jpg\" was not found.");
  104. }
  105. pageContentContext2->q();
  106. pageContentContext2->cm(0.4,0,0,0.4,57.5,241);
  107. pageContentContext2->Do(pdfPage2->GetResourcesDictionary().AddFormXObjectMapping(image->GetObjectID()));
  108. pageContentContext2->Q();
  109. delete image;
  110. // End content context, and write the page
  111. pdfWriterB.EndPageContentContext(pageContentContext2);
  112. pdfWriterB.WritePageAndRelease(pdfPage2);
  113. pdfWriterB.EndPDF();
  114. }