You should not pass the buffer itself as format argument to sprintf. It depends on the implementation (the used standard C library) if it is supported or not. But even if it is supported it is bad style. And in your case it is the reason for the corrupted buffer content here:
sprintf(buffer, "%s\r\n", buffer);
memcpy(buffer + strlen(buffer),content,lSize);
sprintf(buffer, "%s\r\n", buffer);
The PDF file is a binary file. While you use memcpy to append the binary data to your buffer, the next sprintf call stops at the first null byte truncating the buffer. A common solution is to use a buffer offset variable which is incremented by the return value of sprintf:
int ofs = strlen(buffer);
ofs += sprintf(buffer + ofs, "\r\n");
memcpy(buffer + ofs,content,lSize);
ofs += lSize;
ofs += sprintf(buffer + ofs, "\r\n");