1

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.

  • 1
    Tables are created by adding text at certain coordinates, and then drawing lines at certain coordinates. There is no table feature, as such. For images, you might look at "inline images". For a gentle introduction to PDF syntax, try my book: https://www.amazon.co.uk/PDF-Explained-John-Whitington/dp/1449310028/ – johnwhitington Apr 13 '23 at 15:18
  • @KJ : Thank you for the response. The first link is not working. I will look into the cmd file you sent. – Sandhya Yegireddi Apr 18 '23 at 14:05
  • @KJ : I gone through your cmd file and looks good. Do you have a reference or example of drawing outline to text. That will be helpful. Also I found some commands like TH, TR, TD THead, TBody, TFoot etc.. in spec but didn't find clear example anywhere. – Sandhya Yegireddi Apr 18 '23 at 15:41

1 Answers1

0

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

K J
  • 8,045
  • 3
  • 14
  • 36
  • This is really helpful. Thanks a lot for the support. I have one doubt. How can I understand the structure of the below lines wrt PDF SPEC. 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 ; Which section will give more information? What are you using here? – Sandhya Yegireddi May 08 '23 at 15:13
  • With the help of your code, I am able to update the table and text into it. Thanks a lot for the support!!! – Sandhya Yegireddi May 08 '23 at 16:16
  • Sure. This will be helpful!! Thanks!! – Sandhya Yegireddi May 09 '23 at 14:07
  • @K J : Hi again. I am trying to add inline image to the pdf file you created and for some reason I am not able to see the image in the pdf file. I tried by adding as stream object instead of inline image too. I took the flower.pdf shared by you and trying to add it. I am not able upload the file here. I am copying the overview here. q 500.000 000.000 000.000 477.000 170.750 053.875 cm BI /W 500 /H 477 /CS /RGB /BPC 8 /F /DCT ID EI Q – Sandhya Yegireddi May 12 '23 at 14:53
  • Okay. I will try without encoding. – Sandhya Yegireddi May 12 '23 at 15:16
  • I am able to insert image also to the content stream. Copying the image data from flower2.pdf file to another file is changing few chars such as NUL with Space etc.. So it didn't worked. Now I am able to adapt it by modifying the flower2.pdf file. – Sandhya Yegireddi May 18 '23 at 16:49
  • Yeah, I came to know about it after copying. – Sandhya Yegireddi May 19 '23 at 17:53