//+------------------------------------------------------------------+
//|                                                     HelloPdf.mqh |
//|                               Copyright 2026, Sandro Begashvili  |
//|                   https://www.mql5.com/en/users/sandrobegashvil  |
//|                          Telegram: https://t.me/MrMQL5Developer  |
//| Building a PDF Creation Library in MQL5 (Part 2): Why PDFs Break |
//|                                                                  |
//| The known-good file from Part 1, as a function, so that Part 2   |
//| can break copies of it on purpose and still have the original to |
//|  compare against.                                                |
//|                                                                  |
//|  This is demo support, not library code. Nothing in the PDF      |
//|  library will ever include it.                                   |
//+------------------------------------------------------------------+
#ifndef PDF5_HELLOPDF_MQH
#define PDF5_HELLOPDF_MQH

//--- The byte positions Part 1 counted by hand, kept here as named
//--- constants so the breakage demo can talk about them.
#define HELLO_SIZE        592   // total bytes
#define HELLO_OBJ4_AT     247   // where "4 0 obj" starts
#define HELLO_OBJ5_AT     339   // where "5 0 obj" starts
#define HELLO_XREF_AT     409   // where the word "xref" starts

//+------------------------------------------------------------------+
//| Appends one line and the single line feed that ends it.          |
//+------------------------------------------------------------------+
string L(const string text)
  {
   return(text + "\n");
  }

//+------------------------------------------------------------------+
//| Returns hello.pdf: 592 bytes, five objects, no errors.           |
//+------------------------------------------------------------------+
string BuildHelloPdf(void)
  {
   string s = "";

   s += L("%PDF-1.4");

   s += L("1 0 obj");
   s += L("<< /Type /Catalog /Pages 2 0 R >>");
   s += L("endobj");

   s += L("2 0 obj");
   s += L("<< /Type /Pages /Kids [3 0 R] /Count 1 >>");
   s += L("endobj");

   s += L("3 0 obj");
   s += L("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595 842]");
   s += L("   /Resources << /Font << /F1 5 0 R >> >>");
   s += L("   /Contents 4 0 R >>");
   s += L("endobj");

   s += L("4 0 obj");
   s += L("<< /Length 43 >>");
   s += L("stream");
   s += L("BT");
   s += L("/F1 36 Tf");
   s += L("72 720 Td");
   s += L("(Hello, PDF!) Tj");
   s += L("ET");
   s += L("endstream");
   s += L("endobj");

   s += L("5 0 obj");
   s += L("<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>");
   s += L("endobj");

   s += L("xref");
   s += L("0 6");
   s += L("0000000000 65535 f ");
   s += L("0000000009 00000 n ");
   s += L("0000000058 00000 n ");
   s += L("0000000115 00000 n ");
   s += L("0000000247 00000 n ");
   s += L("0000000339 00000 n ");
   s += L("trailer");
   s += L("<< /Size 6 /Root 1 0 R >>");
   s += L("startxref");
   s += L("409");
   s += L("%%EOF");

   return(s);
  }

//+------------------------------------------------------------------+
//| Writes a string to MQL5\Files as raw bytes.                      |
//|                                                                  |
//| FILE_ANSI is what makes FileWriteString put one byte per         |
//| character on disk. Without it the terminal writes UTF-16 and the |
//| file is twice the size, with a zero byte between every letter.   |
//+------------------------------------------------------------------+
bool WriteRawFile(const string name, const string content)
  {
   int handle = FileOpen(name, FILE_WRITE | FILE_BIN | FILE_ANSI);
   if(handle == INVALID_HANDLE)
     {
      PrintFormat("Cannot open %s, error %d", name, GetLastError());
      return(false);
     }

   FileWriteString(handle, content, StringLen(content));
   FileClose(handle);

   PrintFormat("%-28s %4d bytes", name, StringLen(content));
   return(true);
  }

#endif // PDF5_HELLOPDF_MQH
//+------------------------------------------------------------------+
