pdfpdf-generationadobepdf-specification

Manual creation of pdf files without using third party libs


I would like to create a pdf file manually without using any third-party libraries. I need to create a sample invoice format which will have some text, a table and an image with raw pdf objects. I looked into few pages but there is no proper data provided and suggesting using third party libs. It will be really helpful if is some information.

Specification link: https://archive.org/details/pdf320002008/page/n3/mode/2up

I gone through some sections in specification. I am able to create pdf with small text using provided example. Need some help in adding a table and an image into the pdf. In the pdf examples encoding is used so I am not able to understand the table creation. I need raw objects format without any encoding in the file.


Solution

  • Programming PDF in a console is easy, but first you need to write your PDF Header, so for a Single Page that should be simple enough in MS NotePad IDE and to Compile simply press Ctrl+S while using a real time view alongside.

    enter image description here

    For your starter I would use a CMD to simply write the basics then add logos and Images later as they are binary and need more maths, such as using DIR to get their filesize. HOWEVER I am going to say that to make a "Basic" File.pdf acceptable for clients, either needs a parse through Printing or other PDF Lib to tidy up to a compliant level (any single CLI.exe may do).

    %PDF-1.1
    1 0 obj <</Pages 2 0 R/Type/Catalog>>endobj
    2 0 obj <</Type/Pages/Count 2/Kids [3 0 R 5 0 R]>>endobj
    3 0 obj <</Type/Page/Contents 4 0 R/Parent 2 0 R/Resources<</Font<</F1<</Type/Font/BaseFont/Helvetica/Subtype/Type1>>>>>>/MediaBox[0 0 595 842]>>endobj
    4 0 obj <</Length 100>>
    stream 
    BT 
      /F1 20 Tf 30 700 Td (Dear Customer) Tj
    ET
    BT 
      /F1 12 Tf 30 640 Td (Here is your Invoice!) Tj
    ET
    BT 
      /F1 11 Tf 30 620 Td
     (See Page 2 for T & C) Tj
    ET
    1.00 g
     30 550 m 565 550 l S
     30 500 m 565 500 l S
     30 550 m 30 50 l S
     500 550 m 500 50 l S
     565 550 m 565 50 l S
    endstream
    endobj
    
    5 0 obj <</Type/Page/Contents 6 0 R/Parent 2 0 R>>endobj
    6 0 obj <</Length 100>>
    stream
    0.5 g
     100 600 50 75 re f S
     100 600 m 500 600 l S
    endstream
    endobj
    
    trailer
    <</Root 1 0 R/Size 7>>
    %%EOF
    

    PDF is mainly XY co-ordinates (like on a graph) so 30 550 m 565 550 l says go to start at 30,550 then moving to 565,550 draw a line the final S is the command to strike as a stroke so each line is defined exactly where needed from origin at lower left in the pages units (normally at points as 1/72" but accepts decimals) a graph paper sheet with units at say 10/72 " can help draft a template. enter image description here