SaveAsPdf problems

Posted by: martin.bruce on 10 March 2020, 12:05 am EST

  • Posted 10 March 2020, 12:05 am EST

    Hello,

    I'm generating a document and want to save it as a pdf. Locally this works fine but when running from a server I get the following error:

    TextFormat has no associated Font, and a default font could not be found. To avoid this situation, assign an available font to FontCollection.SystemFonts.DefaultFont if it is null.

    I asume the server doesn't have access to certain fonts so I followed what the error recomends by doing the following:



    _fontCollection = new FontCollection {DefaultFont = StandardFonts.Times};
    _fontCollection.RegisterDirectory("./Fonts");

    var font = _fontCollection.FindFamilyName("CorpoS");
    if (font != null)
    {
    _fontCollection.DefaultFont = font;
    }

    _logger.LogInformation($"Default font: {(_fontCollection.DefaultFont != null ? _fontCollection.DefaultFont.FontFamilyName : "not found.")} - Font count: {_fontCollection?.Count}");
    doc.SaveAsPdf(memoryStreamPdf, _fontCollection);


    I still get the same error but my logging logs the following:

    Default font: CorpoS - Font count: 4

    So what am I doing wrong here?
  • Replied 11 March 2020, 7:36 pm EST

    Hi Martin,
    We are sorry for the delayed response on this. We were trying to replicate the issue at our end using the provided code snippet by you assuming you are working with the Grapecity Documents for Word and generating the PDF file.
    Here, we observed that there is not FontCollection class available.

    Hence, could you please share a small demo sample depicting your issue. So that we can replicate the issue at our end and assist you accordingly.

    Regards,
    Manish Gupta
  • Replied 11 March 2020, 8:13 pm EST

    Hello Manish,

    Yes I'm using GrapeCity.Documents.Word and GrapeCity.Documents.Layout both version 3.0.0.42 to generate my documents.

    Please be advised that the issue doesn't arise running localy. It first apears once I have deployed on our test server. Is it posible that the issue arises from not having a license key on the test server?

    Below is a short example of what I do (I hope I got the structure right)):


    FontCollection _fontCollection = new FontCollection {DefaultFont = StandardFonts.Times};

    _fontCollection.RegisterDirectory("./Fonts");

    var font = _fontCollection.FindFamilyName("CorpoS");
    if (font != null)
    {
    _fontCollection.DefaultFont = font;
    }

    GcWordDocument doc = new GcWordDocument(_settings.GrapeCityKey);
    doc.Load(memoryStream); //memoryStream contains a docx templpate.

    var runs = doc.Body.Runs;
    List<PersistentRange> runRanges = new List<PersistentRange>(runs.Count);
    foreach (var run in runs)
    {
    runRanges.Add(run.GetPersistentRange());
    }

    foreach (var rr in runRanges)
    {
    //do a regex match
    if(match)
    {
    var font = rr.ParentRun.Font;
    rr.Clear();
    var r = rr.Runs.Last;

    foreach (Match m in matches)
    {
    r.GetRange().Runs.Insert(value, InsertLocation.After);
    r = SetFont(r, font); //Sets the font on the range above.
    }

    rr.Runs.First.Delete();
    }
    }

    var ms = new MemoryStream();
    doc.Save(ms, DocumentType.Document); //Word document is saved fine.

    var msPdf = new MemoryStream();

    //Error occurs on line below. Doesn't matter wich one I use.
    doc.SaveAsPdf(msPdf, _fontCollection); //doc.SaveAsPdf(msPdf);

  • Replied 12 March 2020, 9:01 pm EST

    Hi,
    Thank you for the provided code snippet. This issue requires further investigation hence we have reported this issue to the concerned team with internal tracking id DOC-1870.

    Regards,
    Manish Gupta
  • Marked as Answer

    Replied 17 March 2020, 6:10 am EST

    Hi Martin,

    Please replace the code at the top of your fragment with this:


    FontCollection.SystemFonts.DefaultFont = StandardFonts.Times;
    FontCollection fontCollection = new FontCollection();
    fontCollection.RegisterDirectory("./Fonts");
    var font = fontCollection.FindFamilyName("CorpoS");
    if (font != null)
    {
    FontCollection.SystemFonts.DefaultFont = font;
    }

    Let me know if this helps.

    Thanks,
    Dmitry.
  • Replied 17 March 2020, 6:15 pm EST

    Hello Dmitry

    That worked. I get my pdf's now.

    Thankyou!

    /Martin
  • Replied 18 March 2020, 1:06 am EST

    Hi Martin,

    Great, thanks for letting me know!

    Cheers,
    Dmitry.
  • Replied 13 January 2021, 5:54 pm EST

    Hi Martin,

    It's time make active reports grate again.

    I builded docker image for reproduce this bug https://hub.docker.com/repository/docker/vahpetr/webdesigner-mvc-core

    docker pull vahpetr/webdesigner-mvc-core:1.0.0


    And created docker-compose for you.

    version: "3.9"

    services:
    reports:
    image: vahpetr/webdesigner-mvc-core:1.0.0
    restart: always
    volumes:
    - reports_resources:/app/resources
    - reports_datasets:/app/datasets
    - reports_templates:/app/templates
    ports:
    - 80:80
    - 443:443

    volumes:
    reports_resources:
    driver: local
    reports_datasets:
    driver: local
    reports_templates:
    driver: local


    Reproduce:
    1/ Create docker-compose.yml file

    2/ docker-compose up -d
    3/ open localhost:80
    4/ open any report, Tablix.rdlx for example
    5/ run preview

    Result:
    Failed rendering document.
    TextFormat has no associated Font, and a default font could not be found. To avoid this situation, assign an available font to FontCollection.SystemFonts.DefaultFont if it is null.

    Expectation:
    No error. Correct preview.

    I think problem in miss default fonts in container. Please add correct handling this error.

    P.S The container is created automatically by the dotnet tool tye https://github.com/dotnet/tye
  • Replied 13 January 2021, 4:49 pm EST

    Hello Peter,

    Are you facing any issue while using the WebDesigner in the Docker Environment? If yes, please share the detail of the issue so that I will help you accordingly.

    Thanks,
    Mohit
  • Replied 13 January 2021, 6:56 pm EST

    Mohit, good afternoon.

    Yes, this is the problem of using WebDesigner in docker environment. I gave you a docker image, docker-compose.yml, instructions for repeating the problem.

    Added video https://youtu.be/6D7_oBb3Sgw
  • Replied 20 January 2021, 1:54 am EST

    Hello Peter,

    Thanks for the image and steps. I am able to reproduce the issue at my end. I have escalated the issue to the development team (AR-25869)and will inform you once I get any information from them.

    Thanks,
    Mohit
  • Replied 18 January 2021, 1:23 am EST

    Hi Petr,

    If I understand the issue correctly, in your server environment GcPdf could not find any system fonts, in this case you need to provide the fonts explicitly to GcPdf, several samples in our demo site show how to do it, e.g.:
    https://www.grapecity.com/documents-api-pdf/demos/features/fonts/font-collection/code-cs
    https://www.grapecity.com/documents-api-pdf/demos/features/fonts/surrogates-port/code-cs
    Hope this helps,
    Dmitry.
  • Replied 20 January 2021, 8:20 pm EST

    Hi all.

    1/ I am install fonts to local machine
    apt install ttf-mscorefonts-installer


    2/ Add volume to default folder
    - /usr/share/fonts/truetype/msttcorefonts:/app/Resources/Fonts

    (/app folder contains project)

    3/ Created file TestFont.rdlx

    <?xml version="1.0" encoding="utf-8"?>
    <Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition">
    <dd:Name xmlns:dd="http://schemas.datadynamics.com/reporting/2005/02/reportdefinition">TestFont.rdlx</dd:Name>
    <Body>
    <ColumnSpacing>0cm</ColumnSpacing>
    <Height>4.546cm</Height>
    <ReportItems>
    <CustomReportItem Name="FormattedText1">
    <Type>FormattedText</Type>
    <Top>0.559cm</Top>
    <Left>1.772cm</Left>
    <Width>13.096cm</Width>
    <Height>3.987cm</Height>
    <ZIndex>1</ZIndex>
    <CustomProperties>
    <CustomProperty>
    <Name>Html</Name>
    <Value><html><body>Test text</body></html></Value>
    </CustomProperty>
    <CustomProperty>
    <Name>Field1</Name>
    <Value>1</Value>
    </CustomProperty>
    </CustomProperties>
    <Custom>
    <ReportStyle>
    <StyleId>b3356016-1bdf-4ff7-a8f1-71b92fec107b</StyleId>
    <StyleName>Default</StyleName>
    </ReportStyle>
    </Custom>
    </CustomReportItem>
    </ReportItems>
    </Body>
    <BottomMargin>2.5cm</BottomMargin>
    <LeftMargin>2.5cm</LeftMargin>
    <PageHeight>29.7cm</PageHeight>
    <PageWidth>21cm</PageWidth>
    <RightMargin>2.5cm</RightMargin>
    <TopMargin>2.5cm</TopMargin>
    <Width>14.868cm</Width>
    <CustomProperties>
    <CustomProperty>
    <Name>Theme</Name>
    <Value>Cordial.default.rdlx-theme</Value>
    </CustomProperty>
    <CustomProperty>
    <Name>DisplayType</Name>
    <Value>Page</Value>
    </CustomProperty>
    <CustomProperty>
    <Name>SizeType</Name>
    <Value>Default</Value>
    </CustomProperty>
    <CustomProperty>
    <Name>PaperOrientation</Name>
    <Value>Portrait</Value>
    </CustomProperty>
    </CustomProperties>
    <dd:ReportThemes xmlns:dd="http://schemas.datadynamics.com/reporting/2005/02/reportdefinition">
    <ThemeUri>Cordial.default.rdlx-theme</ThemeUri>
    </dd:ReportThemes>
    </Report>


    I am obtain error again:

    Failed rendering document.
    TextFormat has no associated Font, and a default font could not be found. To avoid this situation, assign an available font to FontCollection.SystemFonts.DefaultFont if it is null.


    1/ RDLX file not contains default fonts (only default style id).
    2/ Editor not support change fonts for Formatted text component.

    3/Error not show required fonts.

    Please help.

    Available fonts list in `ttf-mscorefonts-installer` dependency:

    • Andale_Mono.ttf

    • andalemo.ttf

    • arialbd.ttf

    • arialbi.ttf

    • Arial_Black.ttf

    • Arial_Bold_Italic.ttf

    • Arial_Bold.ttf

    • Arial_Italic.ttf

    • ariali.ttf

    • arial.ttf

    • Arial.ttf

    • ariblk.ttf

    • comicbd.ttf

    • Comic_Sans_MS_Bold.ttf

    • Comic_Sans_MS.ttf

    • comic.ttf

    • courbd.ttf

    • courbi.ttf

    • Courier_New_Bold_Italic.ttf

    • Courier_New_Bold.ttf

    • Courier_New_Italic.ttf

    • Courier_New.ttf

    • couri.ttf

    • cour.ttf

    • Georgia_Bold_Italic.ttf

    • Georgia_Bold.ttf

    • georgiab.ttf

    • Georgia_Italic.ttf

    • georgiai.ttf

    • georgia.ttf

    • Georgia.ttf

    • georgiaz.ttf

    • impact.ttf

    • Impact.ttf

    • timesbd.ttf

    • timesbi.ttf

    • timesi.ttf

    • Times_New_Roman_Bold_Italic.ttf

    • Times_New_Roman_Bold.ttf

    • Times_New_Roman_Italic.ttf

    • Times_New_Roman.ttf

    • times.ttf

    • trebucbd.ttf

    • trebucbi.ttf

    • Trebuchet_MS_Bold_Italic.ttf

    • Trebuchet_MS_Bold.ttf

    • Trebuchet_MS_Italic.ttf

    • Trebuchet_MS.ttf

    • trebucit.ttf

    • trebuc.ttf

    • Verdana_Bold_Italic.ttf

    • Verdana_Bold.ttf

    • verdanab.ttf

    • Verdana_Italic.ttf

    • verdanai.ttf

    • verdana.ttf

    • Verdana.ttf

    • verdanaz.ttf

    • webdings.ttf

    • Webdings.ttf

  • Replied 21 January 2021, 5:43 am EST

    Hi Petr,

    Having re-read the thread I think the problem is that you're using Docker, so your fonts must reside inside the dockerfile. When running on Linux, GcPdf (which provides PDF export for AR) looks for system fonts in the following directories and their subdirectories:

    • ~/.fonts/

    • /usr/local/share/fonts/

    • /usr/share/fonts/


    So if you can create one of those directories inside your dockerfile, and put the fonts in that directory (or a subdirectory) - this should help. (I am not very familiar with Docker, but I think this should be possible, right?)

    Please let me know whether this helps.

    Thanks,
    Dmitry.
  • Replied 21 January 2021, 6:48 pm EST

    Hello Dmitry!

    Thanks for the answer. Its work!

    Volume
    - /usr/share/fonts:/usr/share/fonts
    work fine!
    You into can also install
    ttf-mscorefonts-installer
    directly into container.

    I suppose that the assembly should look like this, but I can't check it because I don't have a license (I'm considering your project)

    Dockerfile

    ARG PRODUCT_ID
    ARG PRODUCT_SERIAL_KEY
    FROM mcr.microsoft.com/dotnet/sdk:5.0.102-alpine3.12-amd64 AS sdk

    ENV PRODUCT_ID ${PRODUCT_ID}
    ENV PRODUCT_SERIAL_KEY ${PRODUCT_SERIAL_KEY}

    RUN apk --no-cache add --update nodejs npm
    # https://www.grapecity.com/componentone/docs/license/online-license/licensing-aspnet-apps-on-docker.html
    RUN dotnet tool install -g GrapeCity.LicenseManagerTool
    ENV PATH="$PATH:/root/.dotnet/tools"

    WORKDIR /app
    COPY WebDesigner_MVC_Core.sln WebDesigner_MVC_Core.csproj ./
    RUN dotnet restore
    COPY . .

    RUN gclm "${PRODUCT_ID}" -a "${PRODUCT_SERIAL_KEY}"
    RUN dotnet publish ./WebDesigner_MVC_Core.csproj \
    --runtime alpine-x64 \
    --self-contained true \
    --configuration Release \
    /p:PublishTrimmed=true \
    /p:PublishSingleFile=true \
    --output ./out \
    || gclm "${PRODUCT_ID}" -d "${PRODUCT_SERIAL_KEY}"

    FROM mcr.microsoft.com/dotnet/runtime-deps:5.0.2-alpine3.12-amd64 as runtime
    # do need nodeje in runtime?
    # RUN apk --no-cache add --update nodejs npm
    RUN apk --no-cache add --update msttcorefonts-installer fontconfig && \
    update-ms-fonts && \
    fc-cache -f
    COPY --from=sdk /app/out ./app
    WORKDIR /app
    ENTRYPOINT ["./WebDesigner_MVC_Core"]
    # https://www.grapecity.com/activereportsnet/docs/v14/online/licensing-with-pipelines.html
    CMD ["&", "gclm", "${PRODUCT_ID}", "-d", "${PRODUCT_SERIAL_KEY}"]


    Or tye way.
    docker-compose.yml

    version: "3.9"

    services:
    reports:
    image: webdesigner-mvc-core:1.0.0
    restart: always
    volumes:
    - reports_resources:/app/resources
    - reports_datasets:/app/datasets
    - reports_templates:/app/templates
    - reports_aspnet_keys:/root/.aspnet/DataProtection-Keys
    # line below fix font error if fonts not installed into container
    - /usr/share/fonts:/usr/share/fonts
    ports:
    - 80:80
    - 443:443

    volumes:
    reports_resources:
    driver: local
    reports_datasets:
    driver: local
    reports_templates:
    driver: local
    reports_aspnet_keys:
    driver: local


    Run

    dotnet tool install -g Microsoft.Tye --version "0.6.0-alpha.21070.5"
    tye init
    tye build

    # for debian
    apt install ttf-mscorefonts-installer -y

    docker-compose up -d


    Thanks!
  • Replied 22 January 2021, 3:06 am EST

    Hi Petr,

    Thank you for the heads up, glad I could (eventually :) ) help.

    Thanks,
    Dmitry.
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels