pdfHummusTest1.cpp 5.7 KB

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