Html to pdf save technical support request

Hello, using pdf java solution

24.8

Implementing html to pdf save function.

I want to change the save pdfile font

I need a guide for both ways

  1. font file stored on the server

  2. Font files inside the project

Document doc = new Document();
Page page = doc.getPages().add();
PageInfo pageInfo = new PageInfo();
pageInfo.setMargin(new MarginInfo(10, 10, 10, 10));
page.setPageInfo(pageInfo);

HtmlLoadOptions htmlLoadOptions = new HtmlLoadOptions();
HtmlFragment htmlFragment = new HtmlFragment(“…”);
htmlFragment.setHtmlLoadOptions(htmlLoadOptions);

page.getParagraphs().add(htmlFragment);
/*

  • I configured the page to put html of PDF 1 page content,
  • I’m going to generate a total of three page html

*/

PdfSaveOptions options = new PdfSaveOptions();
doc.save(“C:/A.pdf”, options);

@OH_JUNG_EUN

To change the font when saving a PDF from HTML using Aspose.PDF for Java, you can follow these guidelines for both scenarios: using font files stored on the server and using font files included in your project.

1. Using Font Files Stored on the Server

If your font files are stored on the server, you can specify the font path in your code. Here’s how you can do it:

import com.aspose.pdf.Document;
import com.aspose.pdf.HtmlFragment;
import com.aspose.pdf.Page;
import com.aspose.pdf.TextState;
import com.aspose.pdf.HtmlLoadOptions;

public class HtmlToPdfWithServerFont {
    public static void main(String[] args) {
        // Create a new Document
        Document doc = new Document();
        Page page = doc.getPages().add();

        // Load HTML content
        HtmlLoadOptions htmlLoadOptions = new HtmlLoadOptions();
        HtmlFragment htmlFragment = new HtmlFragment("<p style='font-family: CustomFont'>Hello World</p>");
        
        // Set the font from the server
        TextState textState = new TextState();
        textState.setFont("path/to/your/font/CustomFont.ttf"); // Specify the path to your font file
        htmlFragment.setTextState(textState);

        page.getParagraphs().add(htmlFragment);
        
        // Save the PDF
        doc.save("C:/A.pdf");
    }
}

2. Using Font Files Inside the Project

If you have font files included in your project, you can load them similarly. Here’s an example:

import com.aspose.pdf.Document;
import com.aspose.pdf.HtmlFragment;
import com.aspose.pdf.Page;
import com.aspose.pdf.TextState;
import com.aspose.pdf.HtmlLoadOptions;

public class HtmlToPdfWithProjectFont {
    public static void main(String[] args) {
        // Create a new Document
        Document doc = new Document();
        Page page = doc.getPages().add();

        // Load HTML content
        HtmlLoadOptions htmlLoadOptions = new HtmlLoadOptions();
        HtmlFragment htmlFragment = new HtmlFragment("<p style='font-family: ProjectFont'>Hello World</p>");
        
        // Set the font from the project
        TextState textState = new TextState();
        textState.setFont("ProjectFont"); // Use the font name if it's in the project resources
        htmlFragment.setTextState(textState);

        page.getParagraphs().add(htmlFragment);
        
        // Save the PDF
        doc.save("C:/A.pdf");
    }
}

Additional Notes

  • Ensure that the font files are accessible at the specified paths.
  • If you are using custom fonts, you may need to embed them in the PDF to ensure they are displayed correctly on all devices. This can be done by setting the IsEmbedFonts property in the PdfSaveOptions.

By following these examples, you should be able to customize the font used in your PDF documents generated from HTML content. If you need further assistance, feel free to ask!

Sources

[1]: Convert HTML to PDF Files | Aspose.PDF Java PDF Processing API
[2]: Convert HTML to PDF file in Java - Aspose Documentation
[3]: Formatting PDF Document|Aspose.PDF for Java - Aspose Documentation

I solved the font problem well.
Thank you.

But there’s one more problem

The file size stored in Windows was is 500 kb,
The file size stored in Linux was is 30mb.

Does the size of the storage change depending on the server version?

Please let me know if there are any options that I can solve.

Document doc = new Document();
Page page = doc.getPages().add();
PageInfo pageInfo = new PageInfo();
pageInfo.setMargin(new MarginInfo(10, 10, 10, 10));
page.setPageInfo(pageInfo);

HtmlLoadOptions htmlLoadOptions = new HtmlLoadOptions();
HtmlFragment htmlFragment = new HtmlFragment(“…”);
htmlFragment.setHtmlLoadOptions(htmlLoadOptions);

// Set the font from the project
TextState textState = new TextState();
Font font = FontRepository.openFont(“font file”);
textState.setFont(font); // Use the font name if it’s in the project resources
htmlFragment.setTextState(textState);

page.getParagraphs().add(htmlFragment);

/*

  • I configured the page to put html of PDF 1 page content,
  • I’m going to generate a total of three page html
    */

PdfSaveOptions options = new PdfSaveOptions();
doc.save(“C:/A.pdf”, options);

@OH_JUNG_EUN

The file size difference may be due to the server version because different versions of OS/Server store the font encoding in different ways. It does not look like an issue related to the Aspose.PDF. Please feel free to let us know in case you need to ask something about the API.