cancel
Showing results for 
Search instead for 
Did you mean: 

How to store a file in pendrive as pdf in STM32 using Mass Storage Class and FATFS?

KSing.8
Associate II
 
18 REPLIES 18

How create and store a correct pdf file with fatfs ​

Using FATS (file handling commands) of STM32 MCU.

f_open(&myFile, "TEST.TXT", FA_WRITE |FA_OPEN_APPEND);//for text files
f_open(&myFile, "TEST.pdf", FA_WRITE |FA_OPEN_APPEND);//for PDF files 

.pdf get created in my connected pen drive but when i try to open that file it show format of file is not correct.

Do you realise that PDF is a special file format to make documents to be printed always look identical, regardless of the printer?

To create PDF, you would need quite complex special software, which is much more extensive than the memory available in the STM32. For this reason, it is probably not possible to create PDFs on an STM32 in a meaningful way.

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
Ghofrane GSOURI
ST Employee

Hello @KSing.8​ 

To create a project using FatFs in STM32 with CubeMX, follow these steps:

  1. Start by launching CubeMX and creating a new project. Select the STM32 microcontroller that you want to use and configure the project settings as desired.
  2. In the Configuration tab, expand the Connectivity category and select the USB_OTG_FS node. Enable the USB Device mode and select the Mass Storage Class (MSC) option. This will allow the STM32 to act as a mass storage device, such as a USB drive.
  3. Expand the FatFs node and enable the FatFs option. This will add the FatFs library to your project and configure it for use with the STM32.
  4. In the Configuration tab, click the Generate Code button to generate the initial project code. This will create a basic project structure with the necessary files and libraries to use FatFs with the STM32.
  5. Next, you will need to configure the hardware connections for the USB and storage device. Connect the STM32 to the storage device (such as a USB drive) using the appropriate interface (such as USB or SDIO).
  6. In your project code, use the FatFs API functions to initialize the file system, mount the storage device, and perform file operations such as reading and writing files. For example, you can use the f_open function to open a file, the f_write function to write data to the file, and the f_close function to close the file when you are finished.
  7. You can then build and run the project to test the FatFs functionality. You should be able to access the storage device from your computer and read and write files as desired

Thx

Ghofrane

Hi @Ghofrane GSOURI​ 

I have already followed the above mentioned steps but i am not able to create a PDF file using f_open command.

Thanks

Karanvir Singh

Ghofrane GSOURI
ST Employee

Hello @KSing.8 (Community Member)​ 

It's possible that the issue is with the file name or access mode that you are using with the f_open function. Make sure that you are specifying the correct file name, including the ".pdf" extension, and that you are using the correct access mode.

For example, to create a new PDF file with read-write access, you can use the following code:

FIL file;
FRESULT result;
 
result = f_open(&file, "myfile.pdf", FA_CREATE_ALWAYS | FA_WRITE);
 
if (result != FR_OK) {
  // an error occurred, check the error code (f_err)
}

Keep in mind that the FatFs library uses 8.3 file naming conventions, so the maximum length of the file name (excluding the extension) is 8 characters.

Thx

Ghofrane

Peter BENSCH
ST Employee

@KSing.8​ Before we continue to talk past each other, please explain what data you actually write in the file TEST.pdf?

As mentioned above, a PDF file does not contain text, but a binary structure that has been standardised as ISO32000. You had also written earlier:

Using FATS (file handling commands) of STM32 MCU

[...] f_open(&myFile, "TEST.pdf", [...]

.pdf get created in my connected pen drive

so that the file is obviously also created. You can also call this file TEST.exe or TEST.odf, but this does not make it an executable Windows program or an OpenDocument for e.g. LibreOffice.

If you want to create a real PDF, you must also write the appropriate data into the file.

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

@Peter BENSCH​ "If you want to create a real PDF, you must also write the appropriate data into the file."

So i am looking for way to to convert my data lets say any string into data that further gets stored in pdf file and shows my string.Is there any possible way for this..

Thanks

Karanvir Singh

Piranha
Chief II

https://www.microchip.com/forums/m956973.aspx

Anyway, that is not a task for an absolute beginners...